map.js: 定位,下面這個小demo,可以精確到市
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>map</title> <style> #pos{ text-align: center; } #container{ width: 600px; height: 480px; margin: 30px auto; border: 1px solid gray; text-align: center; } </style> <script type="text/javascript" src="js/map.js"></script> </head> <body> <div id="pos"></div> <div id="container"></div> <script type="text/javascript"> //檢查瀏覽器是否支持定位 function check(){ if(navigator.geolocation) alert("支持定位!"); else alert("不支持定位!"); } //check(); window.onload = function(){ var x,y; if (navigator.geolocation) { //百度API功能 var map = new BMap.Map("container"); //地圖中心點 var point = new BMap.Point(x,y); //地圖初始化,參數一: 中心點坐標 參數二: 地圖縮放等級(百度為1~17) map.centerAndZoom(point,12); var geolocation = new BMap.Geolocation();//可精確到市 geolocation.getCurrentPosition(function(r){ //檢索成功的狀態 if (this.getStatus() == BMAP_STATUS_SUCCESS) { //當前位置坐標 var mk = new BMap.Marker(r.point); //addOverlay(): 使用添加圖層的方法, 將當前坐標覆蓋到地圖, mk代表當前坐標 map.addOverlay(mk); //將地圖中心位置移動到當前坐標 map.panTo(r.point); document.getElementById("pos").innerHTML = "您當前的位置: 經度" + r.point.lng +", 緯度" + r.point.lat; } else{ } },{enableHighAccuracy:true})//enableHighAccuracy: 是否使用精確定位, true使用, false不使用 } else{ //getStats(): 獲取狀態 alert("獲取定位失敗!" + this.getStats()); } } </script> </body> </html>
