vue3 使用高德地图实现线路规划功能

1.引入百度api值public文件夹index.html
2.安装依赖

npm install @amap/amap-jsapi-loader
  1. 获取 key 值。
  2. 注册高德账号,注册成功后登录,然后 添加 key 值。复制添加后的 Key 值到组件,就可以使用了。

5.引入组件

import AMapLoader from "@amap/amap-jsapi-loader";
function getadress() {
  window._AMapSecurityConfig = {
    securityJsCode: "f902f26ff2a29756cf51384dda1ec0eb",
  };
  let startLngLat = ref('')
  AMapLoader.load({
    key: "24f840a20d3c6782254aa2fa03a2a8e0", // 申请好的Web端开发者Key,首次调用 load 时必填
    version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
    plugins: [], // 需要使用的插件列表
  }).then((AMap) => {
    // 初始化地图
    map.value = new AMap.Map('dwmap', {
      viewMode: "3D", // 是否为3D地图模式
      zoom: 18, // 初始化地图级别
      center: [106.535698, 29.565219], // 初始化地图中心点位置
    });



    let that = this;
    AMap.plugin("AMap.Geolocation", () => {
      var geolocation = new AMap.Geolocation({
        // 是否使用高精度定位,默认:true
        enableHighAccuracy: true,
        // 设置定位超时时间,默认:无穷大
        timeout: 10000,
      });


      geolocation.getCurrentPosition(function (status, result) {
        console.log("status, result", status, result);
        if (status === "complete") {
          console.log("成功:", JSON.stringify(result.position));
          startLngLat.value = result.position
          // //引入和创建驾车规划插件 这个是高德地图,手机获取位置时很精准
          AMap.plugin(["AMap.Driving"], function () {
            const driving = new AMap.Driving({
              map: map.value,
              panel: "my-panel", //参数值为你页面定义容器的 id 值<div id="my-panel"></div>
            });



            const endLngLat = [106.535698, 29.565219] //终点坐标
            //获取起终点规划线路
            driving.search(startLngLat.value, endLngLat, function (status, result) {
              if (status === "complete") {
                //status:complete 表示查询成功,no_data 为查询无结果,error 代表查询错误
                //查询成功时,result 即为对应的驾车导航信息
                console.log(result);
              } else {

              }
            });
          });
        } else {
          // console.log("获取驾车数据失败:" + result);
          API.get_map().then(res => {
            let rectangle = res.data.data.rectangle
            console.log(rectangle);
            const afterSemicolon = res.data.data.rectangle.split(';')[1];
            const coordinates = afterSemicolon.split(',').map(coord => parseFloat(coord));
            console.log(coordinates);
            AMap.plugin(["AMap.Driving"], function () {
              const driving = new AMap.Driving({
                map: map.value,
                panel: "my-panel", //参数值为你页面定义容器的 id 值<div id="my-panel"></div>
              });



              const endLngLat = [106.535698, 29.565219] //终点坐标
              //获取起终点规划线路
              driving.search(coordinates, endLngLat, function (status, result) {
                if (status === "complete") {
                  //status:complete 表示查询成功,no_data 为查询无结果,error 代表查询错误
                  //查询成功时,result 即为对应的驾车导航信息
                  console.log(result);
                } else {

                }
              });
            });
          })
        }
      });
    });



  }).catch((e) => {
    console.log(e);
  });








}

3.加上销毁事件

onUnmounted(() => {
  map.value?.destroy();
});

最终效果
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

蒜苔肉丝

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

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

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

打赏作者

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

抵扣说明:

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

余额充值