下面的BaiduMap就是百度地圖的實例對象
//關於狀態碼 const BMAP_STATUS_SUCCESS = 0 //檢索成功。對應數值“0”。 const BMAP_STATUS_CITY_LIST = 1 //城市列表。對應數值“1”。 const BMAP_STATUS_UNKNOWN_LOCATION = 2 //位置結果未知。對應數值“2”。 const BMAP_STATUS_UNKNOWN_ROUTE = 3 //導航結果未知。對應數值“3”。 const BMAP_STATUS_INVALID_KEY = 4 //非法密鑰。對應數值“4”。 const BMAP_STATUS_INVALID_REQUEST = 5 //非法請求。對應數值“5”。 const BMAP_STATUS_PERMISSION_DENIED = 6 //沒有權限。對應數值“6”。(自 1.1 新增) const BMAP_STATUS_SERVICE_UNAVAILABLE = 7 //服務不可用。對應數值“7”。(自 1.1 新增) const BMAP_STATUS_TIMEOUT = 8 //超時。對應數值“8”。(自 1.1 新增) //自動定位 var geolocation = new BaiduMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ console.log('您的坐標:' + r.point.lng + ',' + r.point.lat); // 這是百度坐標 var lng = r.point.lng; var lat = r.point.lat; // 創建地理編碼實例 var myGeo = new BaiduMap.Geocoder(); // 根據坐標得到地址描述 myGeo.getLocation(new BaiduMap.Point(lng, lat), function(result){ if (result){ alert('定位成功:'+ result.address); } }); } else { alert('定位失敗:' + this.getStatus()); } })
文檔:
【獲取定位】https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/geoloaction
【地址逆解析】https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/geocoding
