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

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

新时代_打工人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值