vue地图规划

<template>
  <div id="container"></div>
  <div id="panel"></div>
 
  <div style="position: fixed; top: 0; width: 100vw">
    <van-nav-bar
      title="地图"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
  </div>
</template>
 
<script setup>
import { onMounted, onUnmounted, ref } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";
let map = null;
onMounted(() => {
  window._AMapSecurityConfig = {
    securityJsCode: "6763cff122c833d26aa962ad2f433965",
  };
 
  AMapLoader.reset();
  AMapLoader.load({
    key: "5df7186e05e3aa7217ee42921b20dd31", // 申请好的Web端开发者Key
    version: "2.0", // 指定要加载的JSAPI版本
    plugins: ["AMap.Scale", "AMap.Driving"], // 确保加载了AMap.Driving插件
  })
    .then((AMap) => {
      // 创建地图实例
      map = new AMap.Map("container", {
        resizeEnable: true,
        center: [116.397428, 39.90923], // 地图中心点
        zoom: 13, // 地图显示的缩放级别
      });
 
      // 确保插件已正确加载
      AMap.plugin(["AMap.Driving"], function () {
        try {
          var driving = new AMap.Driving({
            map: map,
            //   panel: "panel"
          });
 
          // 使用起终点名称规划驾车导航路线
          driving.search(
            [
              { keyword: "河南省新乡市智慧岛园区" },
              { keyword: "河南省新乡市五龙山动物园" },
            ],
            function (status, result) {
              if (status === "complete") {
                console.log("绘制驾车路线完成");
              } else {
                console.error("获取驾车数据失败:", result);
              }
            }
          );
        } catch (e) {
          console.error("Driving constructor error: ", e);
        }
      });
    })
    .catch((e) => {
      console.log("AMapLoader load error:", e);
    });
});
 
onUnmounted(() => {
  if (map) {
    map.destroy();
  }
});
 
onUnmounted(() => {
  map?.destroy(); // 组件卸载时销毁地图实例
});
 
const onClickLeft = () => history.back(); // 返回按钮的点击事件,返回到上一个页面
</script>
 
<style scoped>
#container {
  margin: 0;
  padding: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
 
#panel {
  position: fixed;
  background-color: white;
  max-height: 90%;
  overflow-y: auto;
  top: 10px;
  right: 10px;
  width: 280px;
}
 
#panel .amap-call {
  background-color: #009cf9;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
 
#panel .amap-lib-driving {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  overflow: hidden;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值