使用高德地图api获取用户浏览器定位

2023-12-27 15:34:02

1.去高德官网注册获取key和密钥

2.导入

import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
  securityJsCode: '',
};

3.vue代码

queryLocation() {
      AMapLoader.load({
        key: '',
        version: '2.0',
        plugins: [, 'AMap.Geolocation', 'AMap.AutoComplete'],
        resizeEnable: true,
      }).then((AMap) => {
        const that = this;
        that.getCurrentLocation(); //获取当前定位
      });
    },
    getCurrentLocation() {
      const that = this;
      that.geolocation = new AMap.Geolocation({
        timeout: 10000,
        enableHighAccuracy: true,
      });
      that.geolocation.getCurrentPosition(function (status, result) {
        if (status == 'complete') {
          that.onComplete(result);
        }
      });
    },
    onComplete(data) {
      let lat = data.position.lat;
      let lng = data.position.lng;
      // let result = wgs84_to_gcj02(lng, lat);
      this.map.panTo([lat, lng]);
      let marker = L.animatedMarker(
        { lat: lat, lng: lng },
        { iconDefault: this.getDefaultVehicle('location', this.factor) }
      );
      marker.addTo(this.map);
    },

文章来源:https://blog.csdn.net/qq_45827004/article/details/135246706
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。