使用百度地图实现详细地址自动补全
生活随笔
收集整理的這篇文章主要介紹了
使用百度地图实现详细地址自动补全
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<!DOCTYPE html>
<html>
<head><meta name="viewport" content="initial-scale=1.0, user-scalable=no" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>地址選擇</title><style type="text/css">body, html{width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";font-size:14px;}#l-map{height:500px;width:100%;}#r-result{width:100%;}</style><script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=EG3bwH1OHAQZMbDYU5Cuq5Zc935lhmFn"></script>
</head><body>
<div id="r-result"><input type="text" id="suggestId" size="20" style="width:500px;height:30px" placeholder="請輸入地址"/><input type="button" οnclick="addrSearch()" class="btn btn-danger" value="搜索" style="width: 150px;height: 35px">
</div>
<div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
<div id="l-map"></div>
<script type="text/javascript">var marker;var zoomSize = 18;var map = new BMap.Map("l-map");var lon = 116.404; //默認(rèn)為北京市var lat= 39.915;//業(yè)務(wù)處理,獲取業(yè)務(wù)中的經(jīng)緯度,有則處理,沒有則用默認(rèn)北京if(typeof getBizPoint == "function"){var pointJson = getBizPoint();lon = pointJson.lon;lat = pointJson.lat;}//確定中心位置var point = new BMap.Point(lon, lat);map.centerAndZoom(point, zoomSize);//標(biāo)注marker = new BMap.Marker(point);// 創(chuàng)建標(biāo)注map.addOverlay(marker); // 將標(biāo)注添加到地圖中marker.enableDragging(); // 可拖拽marker.addEventListener("dragend", setBizValue);// 添加帶有定位的導(dǎo)航控件var navigationControl = new BMap.NavigationControl({// 靠左上角位置anchor: BMAP_ANCHOR_TOP_LEFT,// LARGE類型type: BMAP_NAVIGATION_CONTROL_LARGE,// 啟用顯示定位enableGeolocation: true});map.addControl(navigationControl);var geoc = new BMap.Geocoder();//輸入地址事件處理 startvar ac = new BMap.Autocomplete( //建立一個自動完成的對象{"input" : "suggestId","location" : map});ac.addEventListener("onhighlight", function(e) { //鼠標(biāo)放在下拉列表上的事件var str = "";var _value = e.fromitem.value;var value = "";if (e.fromitem.index > -1) {value = _value.province + _value.city + _value.district + _value.street + _value.business;}str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;value = "";if (e.toitem.index > -1) {_value = e.toitem.value;value = _value.province + _value.city + _value.district + _value.street + _value.business;}str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;G("searchResultPanel").innerHTML = str;});var myValue;ac.addEventListener("onconfirm", function(e) { //鼠標(biāo)點(diǎn)擊下拉列表后的事件var _value = e.item.value;myValue = _value.province + _value.city + _value.district + _value.street + _value.business;G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;setPlace();});function setPlace(){map.clearOverlays(); //清除地圖上所有覆蓋物function myFun(){var pp = local.getResults().getPoi(0).point; //獲取第一個智能搜索的結(jié)果setBizValueForPoint(pp);map.centerAndZoom(pp, zoomSize);marker = new BMap.Marker(pp);marker.enableDragging(); // 可拖拽map.addOverlay(marker); //添加標(biāo)注marker.addEventListener("dragend", setBizValue);}var local = new BMap.LocalSearch(map, { //智能搜索onSearchComplete: myFun});local.search(myValue);}function G(id) {return document.getElementById(id);}//輸入地址事件處理 end//地址轉(zhuǎn)坐標(biāo)function addrSearch(serachAddr){// 創(chuàng)建地址解析器實(shí)例var myGeo = new BMap.Geocoder();// 將地址解析結(jié)果顯示在地圖上,并調(diào)整地圖視野if(!serachAddr){serachAddr = $("#suggestId").val();}myGeo.getPoint(serachAddr, function(point){if (point) {setBizValueForPoint(point);map.clearOverlays();map.centerAndZoom(point, zoomSize);marker = new BMap.Marker(point);marker.enableDragging(); // 可拖拽map.addOverlay(marker); //添加標(biāo)注marker.addEventListener("dragend", setBizValue);}else{console.log("search click no results!")}});}//業(yè)務(wù)方法 start//根據(jù)事件,設(shè)置經(jīng)緯度和地址function setBizValue(e){var point = e.point;setBizValueForPoint(point);}//根據(jù)Point,設(shè)置經(jīng)緯度和地址function setBizValueForPoint(point){lon = point.lng;lat = point.lat;geoc.getLocation(point, function(rs){var addComp = rs.addressComponents;addr = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;if(typeof setBizFun == "function"){setBizFun({lon:lon,lat:lat,addr:addr});}});}//根據(jù)marker,設(shè)置經(jīng)緯度和地址function setBizValueForMarker(){var point = marker.getPosition();setBizValueForPoint(point);}//end</script>
</body>
</html>
?
簡潔版的檢索
?
<!DOCTYPE html> <html> <head><meta name="viewport" content="initial-scale=1.0, user-scalable=no" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>地址選擇</title><style type="text/css">body, html{width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";font-size:14px;}#r-result{width:100%;}</style><script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=EG3bwH1OHAQZMbDYU5Cuq5Zc935lhmFn"></script> </head><body> <div id="r-result"><input type="text" id="suggestId" size="20" style="width:500px;height:30px" placeholder="請輸入地址"/><input type="button" οnclick="addrSearch()" class="btn btn-danger" value="搜索" style="width: 150px;height: 35px"> </div> <div id="searchResultPanel" style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div> <div id="l-map"></div> <script type="text/javascript">var marker;var zoomSize = 18;var map = new BMap.Map("l-map");var lon = 116.404; //默認(rèn)為北京市var lat= 39.915;//業(yè)務(wù)處理,獲取業(yè)務(wù)中的經(jīng)緯度,有則處理,沒有則用默認(rèn)北京if(typeof getBizPoint == "function"){var pointJson = getBizPoint();lon = pointJson.lon;lat = pointJson.lat;}//確定中心位置var point = new BMap.Point(lon, lat);map.centerAndZoom(point, zoomSize);//標(biāo)注marker = new BMap.Marker(point);// 創(chuàng)建標(biāo)注marker.addEventListener("dragend", setBizValue);// 添加帶有定位的導(dǎo)航控件var navigationControl = new BMap.NavigationControl({// 靠左上角位置anchor: BMAP_ANCHOR_TOP_LEFT,// LARGE類型type: BMAP_NAVIGATION_CONTROL_LARGE,// 啟用顯示定位enableGeolocation: true});//輸入地址事件處理 startvar ac = new BMap.Autocomplete( //建立一個自動完成的對象{"input" : "suggestId","location" : map});ac.addEventListener("onhighlight", function(e) { //鼠標(biāo)放在下拉列表上的事件var str = "";var _value = e.fromitem.value;var value = "";if (e.fromitem.index > -1) {value = _value.province + _value.city + _value.district + _value.street + _value.business;}str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;value = "";if (e.toitem.index > -1) {_value = e.toitem.value;value = _value.province + _value.city + _value.district + _value.street + _value.business;}str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;G("searchResultPanel").innerHTML = str;});var myValue;ac.addEventListener("onconfirm", function(e) { //鼠標(biāo)點(diǎn)擊下拉列表后的事件var _value = e.item.value;myValue = _value.province + _value.city + _value.district + _value.street + _value.business;G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;setPlace();});function G(id) {return document.getElementById(id);}//業(yè)務(wù)方法 start//根據(jù)事件,設(shè)置經(jīng)緯度和地址function setBizValue(e){var point = e.point;setBizValueForPoint(point);} </script> </body> </html>
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/fjkgrbk/p/baidu_map.html
總結(jié)
以上是生活随笔為你收集整理的使用百度地图实现详细地址自动补全的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html head
- 下一篇: 基于vue-cli配置手淘的lib-fl