当前位置: 首页 > article >正文

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

在这里插入图片描述


http://www.kler.cn/news/160505.html

相关文章:

  • 物联网安全芯片ACL16 采用 32 位内核,片内集成多种安全密码模块 且低成本、低功耗
  • 【1】基于多设计模式下的同步异步日志系统-项目介绍
  • 5-redis高级-哨兵
  • vue3 Hooks函数使用及常用utils封装
  • LeetCode双指针:第一个错误的版本
  • Redis Reactor事件驱动模型源码
  • Linux-centos上如何配置管理NFS服务器?
  • 数据分析中的绝地反击:如何解救一个陷入困境的数据模型
  • IDEA切换Python虚拟环境
  • Vue3计算属性与监听属性和生命周期
  • Linux网卡命名规则
  • Spring Boot学习(三十三):集成kafka
  • 让关节远离疼痛,重拾健康活力
  • Java架构师系统架构设计原则应用
  • 13款趣味性不错(炫酷)的前端动画特效及源码(预览获取)分享(附源码)
  • 解决Error:You‘re using an RSA key with SHA-1, which is no longer allowed
  • 多关键字排序(java实训)
  • HarmonyOS4.0从零开始的开发教程04 初识ArkTS开发语言(下)
  • 机器学习---pySpark案例
  • 深入理解Vue.js中的this:解析this关键字及其使用场景
  • uniapp实战 —— 分类导航【详解】
  • 设置webstorm和idea符合Alibaba规范
  • 【Docker】从零开始:17.Dockerfile基本概念
  • 指定分隔符对字符串进行分割 numpy.char.split()
  • 自然语言处理(NLP)技术-AI生成版
  • Flinksql bug :Illegal mixing of types in CASE or COALESCE statement
  • 按天批量创建间隔分区表(DM8:达梦数据库)
  • 【PTA-C语言】编程练习4 - 数组Ⅰ
  • HarmonyOS4.0从零开始的开发教程08构建列表页面
  • 17、XSS——session攻击