H5百度地圖獲取定位和地址信息


下面的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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM