百度地图地理定位

function getLocation() {
    // 创建查询对象
    var geolocation = new BMap.Geolocation();
    //调用getCurrentPosition函数
    geolocation.getCurrentPosition(function(r){
          console.log(this.getStatus());
          // 如果查询成功
          if(this.getStatus() == 0){
              // 地理位置逆解析
              var geoc = new BMap.Geocoder();
              geoc.getLocation(r.point,function(rs){
                  var address = rs.addressComponents;
                  var addComp = rs.addressComponents;
                  currentCity = addComp.province.slice(0,-1) + "," + a.city + "," + addComp.district ;
                  console.log(currentCity);
                  if(rs.surroundingPois.length!==0){
                      currentLocation = rs.surroundingPois[0].title;
                  }else if(rs.surroundingPois.length==0){
                      if(rs.business!==''){
                          currentLocation = rs.business.split(',')[0];
                      }else if(rs.business==''){
                          currentLocation = addComp.city;
                      }
                  }
          }else {
              console.log("定位失败!")
          }
      },{enableHighAccuracy: true});

}

猜你喜欢

转载自www.cnblogs.com/ytwanzi/p/9258919.html