vue2+typescript使用高德地图2.0版本

高德地图 webjs api 2.0官网教程

AMap.Driving使用说明

在这里插入图片描述

<div class="mmp">
      <div id="map" ref="mapcontainer"></div>
 </div>
 
 <script lang="ts">
//安全密钥
window._AMapSecurityConfig={
   
  securityJsCode: "高德地图key密钥",
}
import {
    Component, Emit, Vue } from "vue-property-decorator";
import AMapLoader from "@amap/amap-jsapi-loader";
@Component
export default class HomeView extends Vue {
   
  AMap: any = undefined;
  map: any = undefined;
  start: any = [];
  end: any = [];
  zoom: number = 10;
  getInMap() {
   
    AMapLoader.load({
   
      key: "高德地图key值",
      version: "2.0",
      plugins: ["AMap.Scale"],
    })
      .then((AMap: any) => {
   
        this.map = new AMap.Map("map", {
   
          resizeEnable: true,
          zoom: this.zoom,
          center: this.start, //地图中心点
        });
        this.AMap = AMap;
        this.initMap(AMap);
      })
      .catch((e) => {
   
        console.error(e);
      });
  }
  initMap(AMap: any) {
   
    this.map.on("zoomchange", () => {
   
      var zoom = this.map.getZoom();
      this.zoom = zoom;
    });
    this.map.on("zoomend", () => {
   });

    var that = this;
    // 添加起点和终点
    const startMarker = new AMap.Marker({
   
      position: that.start,
    });
    const endMarker = new AMap.Marker({
   
      position: that.end,
    });
    that.map.add(startMarker);
    that.map.add(endMarker);
    // 绘制路线
    AMap.plugin(["AMap.Driving"], function () {
   
      var driving = new AMap.Driving({
   
        map: that.map,
        policy: AMap.DrivingPolicy.LEAST_TIME,
        // panel: "panel",
      });
      driving.search(
        new AMap.LngLat(that.start[0], that.start[1]),
        new AMap.LngLat(that.end[0], that.end[1]),
        function (status: any, result: any) {
   
          if (status === "complete") {
   
            console.log("绘制驾车路线完成");
          } else {
   
            console.log("获取驾车数据失败:" + result);
          }
        }
      );
    });
  }

  mounted() {
   
    let a: any = this.$route.query.a;//"120.111111,45.111111"
    let b: any = this.$route.query.b;//"120.111111,45.111111"
    this.start = a.split(",");
    this.end = b.split(",");
    this.getInMap();
  }
}
</script>
<style lang="scss" scoped>
#map {
   
  width: 100%;
  height: calc(100vh - 46px);
  background-color: rgb(238, 235, 236);
}
.mmp {
   
  display: flex;
  justify-content: space-between;
}
#panel {
   
  width: 20%;
  height: 100%;
}
</style>

自定义声明window(shims-vue.d.ts)
declare module "*.vue" {
   
  import Vue from "vue";
  export default Vue;
}
interface Window {
   
  _AMapSecurityConfig: {
   
    securityJsCode: string;
  }
}
declare let window: Window

在这里插入图片描述

最近更新

  1. TCP协议是安全的吗?

    2023-12-06 23:38:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-06 23:38:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-06 23:38:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-06 23:38:04       20 阅读

热门阅读

  1. 基于Boost::Beast模块的小型HTTP服务器编程

    2023-12-06 23:38:04       34 阅读
  2. Spark_spark参数配置优先级

    2023-12-06 23:38:04       39 阅读
  3. spark写入数据报错

    2023-12-06 23:38:04       36 阅读
  4. pymysql的基本用法

    2023-12-06 23:38:04       41 阅读
  5. 网络数据通信—ProtoBuf实现序列化和反序列化

    2023-12-06 23:38:04       38 阅读
  6. git小白初学习

    2023-12-06 23:38:04       29 阅读
  7. 让 OpenAI GPT4 出 10 道题测试其他开源大语言模型

    2023-12-06 23:38:04       24 阅读
  8. 什么是DDI?DDI的原理和作用是什么?一文看懂

    2023-12-06 23:38:04       36 阅读
  9. USTC Fall2023 高级人工智能期末考试回忆版

    2023-12-06 23:38:04       39 阅读
  10. 力扣labuladong一刷day29天二叉树

    2023-12-06 23:38:04       40 阅读
  11. 还记得当初自己为什么选择计算机?

    2023-12-06 23:38:04       32 阅读
  12. Spring第四课,MVC终章,应用分层的好处,总结

    2023-12-06 23:38:04       32 阅读