Vue 中使用高德地图API 获取地理位置+逆地理编码

库版本
packageversion
vue2.6.0
vant2.12.6
@amap/amap-jsapi-loader1.0.1
需求:

H5中获得用户位置授权后获取终端经纬度和地点名称,PC/移动端适用。

Action!

第一步:

引入高德API库:

import AMapLoader from '@amap/amap-jsapi-loader';
第二步:

添加插件,绘制地图

  • AMap.Geocoder 地理编码与逆地理编码
  • AMap.Geolocation 高德基于浏览器API的定位插件
<template>
	<section id="amapContainer" />
</template>
<script>
export default {
  name: 'AmapGeomaker',
  mounted() {
    AMapLoader.load({
      // 高德开发者密钥 :平台申请
      key: AmapKey,
      // 高德API版本: 2.0
      version: AmapApiVersion,
      // 加载高德内置插件
      plugins: ['AMap.Geocoder', 'AMap.Geolocation']
    })
      .then((AMap) => {
        // 绘制地图
        const AMAP = new AMap.Map('amapContainer', {
          resizeEnable: true
        });

        // Geolocation 配置
        const OPTIONS = {
          // 设置定位超时时间,默认:无穷大
          timeout: 4000,
          // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
          zoomToAccuracy: true
        };

        const Geolocation = new AMap.Geolocation(OPTIONS);
        const Geocoder = new AMap.Geocoder();

        AMAP.addControl(this.geolocation);

        // 获取定位信息
        Geolocation.getCurrentPosition((status, result) => {
          const { status: statusCode, originMessage } = result;
          // 成功回调 status 返回0
          if (status === 'complete' && !statusCode) {
            // KL:高精度纬度
            // kT:高精度经度(注意大小写,高德返回 kT)
            const { position: { KL, kT } } = result;

            // 根据获取到的经纬度进行逆地理编码
            Geocoder.getAddress([KL, kT], (status, { regeocode }) => {
              if (status === 'complete' && regeocode) {
                // address即经纬度转换后的地点名称
                const address = regeocode?.formattedAddress;
              }
            });
          } else {
            // 失败回调 status 统一返回1
            switch (originMessage) {
              case 'Timeout expired':
                // 超时
                break;
              case 'User denied Geolocation':
                // 拒绝授权
                break;
              case 'error Network location provider at \'https://www.googleapis.com/\' : No response received.':
                // 不支持定位,定位不可用的浏览器
                break;
              default:
                // 其他错误
                return;
            }
          }
        });
      })
      .catch((err) => {
        console.error(err)
      });
  }
}
</script>

插件详细使用说明参考:

AMap.Geocoder
AMap.Geolocation

业务效果:

在这里插入图片描述
业务逻辑多,只抽离出一部分,如果在使用过程中遇到问题,欢迎留言~

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Vue3使用高德地图地理编码,可以按照以下步骤进行: 1. 在项目引入高德地图的JS API,可以在index.html添加以下代码: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 2. 在Vue3的组件,可以使用`mounted`钩子函数来初始化地图,并获取当前位置的经纬度: ```javascript mounted() { var map = new AMap.Map('map-container', { zoom: 13 }); var geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为false timeout: 10000, // 超过10秒后停止定位,默认:无穷大 }); geolocation.getCurrentPosition(function(status, result) { if (status == 'complete') { var lng = result.position.lng; var lat = result.position.lat; // 在这里可以进行地理编码 } else { console.log('定位失败'); } }); } ``` 3. 获取到当前位置的经纬度后,可以使用`AMap.Geocoder`类进行地理编码,从而获取当前位置的地址信息: ```javascript var geocoder = new AMap.Geocoder({ city: "全国", // 城市,默认:“全国” radius: 1000 // 范围,默认:500 }); geocoder.getAddress([lng, lat], function(status, result) { if (status == 'complete' && result.regeocode) { var address = result.regeocode.formattedAddress; console.log(address); } else { console.log('地理编码失败'); } }); ``` 在上述代码,`[lng, lat]`表示当前位置的经纬度数组,`result.regeocode.formattedAddress`表示地理编码获取到的地址信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超喜欢你呦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值