百度地圖Api定位當前的位置


<!
doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>百度地圖</title> <script type="text/javascript" src="http://api.map.baidu.com/api?ak=PlhFWpA02aoURjAOpnWcRGqw7AI8EEyO&v=2.0&services=false"></script> </head> <body> <style>   body, html{width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";}   #allmap{height:500px;width:100%;} </style> <div id="allmap"></div> <script type="text/javascript"> $(function(){ var map = new BMap.Map("allmap"); var point = new BMap.Point(116.501573, 39.900877); map.centerAndZoom(point, 16) // 定位對象 var geoc = new BMap.Geocoder(); var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); map.panTo(r.point); //$("#start_point").val(r.point.lng+','+r.point.lat); alert("當前位置經度爲:"+r.point.lng+"緯度爲:"+r.point.lat); setLocation(r.point); }else { $("#start").attr("placeholder","請輸入您的當前位置") alert('沒法定位到您的當前位置,導航失敗,請手動輸入您的當前位置!'+this.getStatus()); } },{enableHighAccuracy: true}); //獲取地理位置的函數 function setLocation(point){ geoc.getLocation(point, function(rs){ var addComp = rs.addressComponents; var result = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber; //$("#start").val(result); //$("#start_location").val(result); alert("當前的位置爲:"+result); }); } }); </script> </body> </html>
相關文章
相關標籤/搜索