VUE实现高德地图轨迹绘制

20 篇文章 0 订阅

高德地图官方API参考地址:概述-地图 JS API | 高德地图API

开发步骤:
安装依赖

npm install vue-amap -S

 main.js中注册

import AMap from 'vue-amap'
Vue.use(AMap)
AMap.initAMapApiLoader({
  key: '你申请的key',
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],
  // 默认高德 sdk 版本为 1.4.4
  v: '1.4.4'
})

map.vue

<template>
   <div id="container" style="width:100%;height:90vh" />
</template>

<script>
// 绘制线路需要的坐标
var lineArr = [[116.478935, 39.997761], [108.983569, 34.285675], [103.85094, 35.987496], [106.205794, 38.458831], [111.761777, 40.875595]]
export default {
  data () {
    return {
      firstArr: [108.983569, 34.285675] // 中心点/初始坐标
    }
  },
  created () {},
  mounted () {
    setTimeout(() => {
      this.initMap() // 异步加载(否则报错initMap is not defined)
      // this.initroad()
    }, 1000)
  },

  methods: {
    // 初始化地图
    initMap () {
      var that = this
      this.map = new AMap.Map('container', {
        resizeEnable: true, // 窗口大小调整
        center: this.firstArr, // 中心 firstArr: [116.478935, 39.997761],
        zoom: 5
      })
      // 添加maker
      this.marker = new AMap.Marker({
        map: this.map,
        position: this.firstArr,
        icon: 'https://webapi.amap.com/images/car.png',
        offset: new AMap.Pixel(-26, -13), // 调整图片偏移
        autoRotation: true, // 自动旋转
        angle: -90 // 图片旋转角度
      })
      that.initroad()
    },
    // 初始化轨迹
    initroad () {
      // 绘制还未经过的路线
      this.polyline = new AMap.Polyline({
        map: this.map,
        path: lineArr,
        showDir: true,
        strokeColor: '#77DDFF', // 线颜色--浅蓝色
        // strokeOpacity: 1,     //线透明度
        strokeWeight: 6, // 线宽
        // strokeStyle: "solid"  //线样式
        lineJoin: 'round' // 折线拐点的绘制样式
      })
      // 绘制路过了的轨迹
      var passedPolyline = new AMap.Polyline({
        map: this.map,
        strokeColor: '#00BBFF', // 线颜色-深蓝色
        path: [[116.478935, 39.997761], [108.983569, 34.285675]],
        // strokeOpacity: 1,     //线透明度
        strokeWeight: 6 // 线宽
        // strokeStyle: "solid"  //线样式
      })
      this.map.setFitView() // 合适的视口
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

效果图 

  • 4
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
实时路线轨迹实现需要结合高德地图的定位功能和绘制功能。 1. 首先,在Vue中引入高德地图JavaScript API,并创建地图对象: ```javascript import AMapLoader from '@amap/amap-jsapi-loader' // 创建地图对象 let map = null AMapLoader.load({ key: 'your amap key', version: '2.0', plugins: ['AMap.Geolocation'] }).then((AMap) => { map = new AMap.Map('map-container') }) ``` 2. 调用高德地图的定位功能获取当前位置: ```javascript let geolocation = null // 创建定位对象 AMapLoader.load({ key: 'your amap key', version: '2.0', plugins: ['AMap.Geolocation'] }).then((AMap) => { geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为false timeout: 10000, // 超过10秒后停止定位,默认为无穷大 buttonOffset: new AMap.Pixel(10, 20), // 定位按钮的位置偏移量 }) map.addControl(geolocation) // 获取当前位置 geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,将地图中心设置为当前位置 map.setCenter(result.position) } else { // 定位失败 console.log('定位失败', result.message) } }) }) ``` 3. 在地图上绘制路线轨迹: ```javascript let path = [] // 路线轨迹点集合 // 添加路线轨迹 let polyline = new AMap.Polyline({ path, strokeColor: '#00a1fe', strokeWeight: 5, }) polyline.setMap(map) // 更新路线轨迹 function updatePath(newPoint) { path.push(newPoint) polyline.setPath(path) } ``` 4. 实时更新路线轨迹: ```javascript // 开启定时器,每隔1秒更新路线轨迹 setInterval(() => { // 获取当前位置 geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,更新路线轨迹 updatePath(result.position) } else { // 定位失败 console.log('定位失败', result.message) } }) }, 1000) ``` 以上就是Vue高德地图实现实时路线轨迹的基本流程,需要注意的是,高德地图JavaScript API使用了Promise异步机制,需要注意回调函数的执行时机。另外,在实际应用中,还需要结合后端接口获取历史路线轨迹数据,并将其加载到地图上。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值