Vue2高德地图实现轨迹

1、创建标签

<div id="container"></div>

2、下包

npm i @amap/amap-jsapi-loader

3、引入@amap/amap-jsapi-loader和高德申请的秘钥

window._AMapSecurityConfig = {
  securityJsCode: "秘钥",
};
import AMapLoader from "@amap/amap-jsapi-loader";

3、引入高德地图的key,初始化地图

 data() {
    return {
      map: null, //初始化 map 对象
      markers: [], // 存储标记
      polyline: null, //存储划线
      trackPoints: [//自定义点的位置
        [116.44, 39.94],
        [116.45, 39.94],
        [116.45, 39.92],
        [116.44, 39.92],
        [116.44, 39.9],
      ],
    };
  },
  mounted() {
    this.initMapWithData();
  },
  methods: {
    initMapWithData() {
const MAP_PLUGINS = [
  "AMap.Scale",
  "AMap.ToolBar",
  "AMap.ControlBar",
  "AMap.HawkEye",
  "AMap.Circle",
  "AMap.Polygon",
  "AMap.Polyline",
  "AMap.Marker",
  "AMap.Pixel",
  "AMap.InfoWindow",
  "AMap.Icon",
];
      AMapLoader.load({
        key: "申请的key",
        version: "2.0",
        plugins: MAP_PLUGINS,
      }).then((AMap) => {
        const map = new AMap.Map("container", {
          viewMode: "3D",
          terrain: true,
          zoom: 11,
          center: [116.41, 39.91],
        });
        this.map = map;
        this.trajectory(); //轨迹
      });
    },
  }

4、轨迹代码如下:   trackPoints是存放轨迹的经纬度

    // 轨迹
    trajectory() {
      this.addStartPointMarker(); //开始的点
      this.addPolyline(); //轨迹的线
      this.addEndPointMarker(); //结束的点
      this.map.setFitView();
    },
    addStartPointMarker() {
      const icon = new AMap.Icon({
        size: new AMap.Size(20, 30), // 设置图标的宽度和高度
        imageSize: new AMap.Size(20, 30), // 设置图标显示时的大小
        image: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", // 自定义图标的 URL
      });
      const startPoint = new AMap.Marker({
        position: this.trackPoints[0],
        map: this.map,
        icon: icon,
        offset: new AMap.Pixel(-10, -30),
      });
      this.markers.push(startPoint);
    },
    addEndPointMarker() {
      const lastPoint = this.trackPoints[this.trackPoints.length - 1];
      const icon = new AMap.Icon({
        size: new AMap.Size(20, 30), // 设置图标的宽度和高度
        imageSize: new AMap.Size(20, 30), // 设置图标显示时的大小
        image: "https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png", // 自定义图标的 URL
      });
      const endPoint = new AMap.Marker({
        position: lastPoint,
        map: this.map,
        icon: icon,
        offset: new AMap.Pixel(-10, -30),
      });
      this.markers.push(endPoint);
    },
    addPolyline() {
      this.polyline = new AMap.Polyline({
        path: this.trackPoints,
        isOutline: true,
        outlineColor: "#ffeeee",
        borderWeight: 2,
        strokeColor: "#0091ff",
        strokeOpacity: 0.9,
        strokeWeight: 6,
        lineJoin: "round",
      });
      this.map.add(this.polyline);
    },

效果如下:

  • 16
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值