ionic3获取经纬度,然后通过高德地图转换为城市名称

1、安装插件

cordova plugin add cordova-plugin-geolocation

2、申请高德地图key

https://lbs.amap.com/

3、index.html界面引入高德地图js文件


  

4、获取经纬度

ionic3获取经纬度,然后通过高德地图转换为城市名称_第1张图片

declare let AMap;

public lPosition: any = "";
public rPosition: any = "";
public formattedAddress: string = "";

// 获取经纬度,转换地址。
getPositionFun() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        console.log(position);
        console.log('纬度: ' + position.coords.latitude + '\n' +
          '经度: ' + position.coords.longitude + '\n' +
          '海拔: ' + position.coords.altitude + '\n' +
          '水平精度: ' + position.coords.accuracy + '\n' +
          '垂直精度: ' + position.coords.altitudeAccuracy + '\n' +
          '方向: ' + position.coords.heading + '\n' +
          '速度: ' + position.coords.speed + '\n' +
          '时间戳: ' + position.timestamp + '\n');
        console.log(AMap);
        // 外面定义获取经纬度的变量,方便界面显示。
        this.lPosition = position.coords.latitude;
        this.rPosition = position.coords.longitude;

        AMap.service("AMap.Geocoder", () => {
          let geocoder = new AMap.Geocoder({
            city: "010"
          });
          console.log(geocoder, "fuwu");
          let positionInfo = [-position.coords.longitude + "", position.coords.latitude + ""];
          console.log(positionInfo);
          geocoder.getAddress(positionInfo, (status, result) => {
            console.log(status, result, "转换定位信息");
            if (status === 'complete' && result.info === 'OK') {
              //获得了有效的地址信息:
              console.log(result.regeocode.formattedAddress);
              this.formattedAddress = result.regeocode.formattedAddress;
            } else {
              //获取地址失败
              console.log("获取地址失败");
            }
          })
        })
      }, this.onError, { timeout: 30000 });
  }

  //定位数据获取失败响应
  onError(error) {
    alert('code: ' + error.code + '\n' +
      'message: ' + error.message + '\n');
  }

你可能感兴趣的:(ionic3获取经纬度,然后通过高德地图转换为城市名称)