vue + uniapp + 高德地图实现微信小程序地图polyline、marker展示

javascript 复制代码
1. 使用uniapp 提供的map组件作为承载页面
 <map
        id="store-order-map"
        :polyline="polyline"
        :markers="markers"
        :include-points="includePoints"
        :scale="10"
      ></map>
javascript 复制代码
 data() {
    return {
      mapCtx: null
      }
   }
javascript 复制代码
 mounted() {
    this.mapCtx = uni.createMapContext('store-order-map', this)
  },
javascript 复制代码
获取路径方法
  fetchRoutePolyline(options) {
      const { start, end, waypoints } = options
      const defaultOptions = {
        key: '你的key',
        origin: start,
        destination: end,
        waypoints: waypoints,
        strategy: 0,
        show_fields: 'tmcs,polyline'
      }
      return new Promise((resolve, reject) => {
        uni.request({
          url: 'https://restapi.amap.com/v3/direction/driving',
          data: defaultOptions,
          success: (res) => {
            resolve(res.data)
          },
          fail: (err) => {
            reject(err)
          }
        })
      })
    },
javascript 复制代码
 生成路径方法
async getPolyline() {
      const { dc_lon, dc_main, stop_lon, stop_main } = this.info
      const start = `${dc_lon},${dc_main}`
      const end = `${stop_lon},${stop_main}`
      const waypoints = []
      try {
        const polylineData1 = await this.fetchRoutePolyline({
          start,
          end,
          waypoints
        })
        const polyline1 = this.generatePolyline(polylineData1, '#67c23a')
        this.polyline = [polyline1]
      } catch (error) {
        uni.showToast({
          title: error.message || '获取路径失败',
          icon: 'none',
          duration: 2000
        })
      }
    }
    generatePolyline(data, color = '#1684fc') {
      const { route } = data
      if (!route) {
        return []
      }
      const path = route.paths[0]
      const steps = path.steps
      const result = []
      steps.forEach((step) => {
        const a = step.polyline.split(';')
        a.forEach((item) => {
          const b = item.split(',')
          const c = {
            longitude: parseFloat(b[0]),
            latitude: parseFloat(b[1])
          }
          result.push(c)
        })
      })
      const line = {
        points: result,
        color,
        width: 6
      }
      return line
    },
javascript 复制代码
创建marker
  createMarkers() {
      const { dc_lon, dc_main, stop_lon, stop_main, car_lat, car_lon } =
        this.info
      const startMarker = {
        id: 1,
        longitude: parseFloat(dc_lon),
        latitude: parseFloat(dc_main),
        iconPath: '/static/icon/xx.png',
        width: 24,
        height: 24
      }
      const endMarker = {
        id: 2,
        longitude: parseFloat(stop_lon),
        latitude: parseFloat(stop_main),
        iconPath: '/static/icon/xxx.png',
        width: 24,
        height: 24
      }
      const carMarker = {
        id: 3,
        longitude: parseFloat(car_lon),
        latitude: parseFloat(car_lat),
        iconPath: '在线地址',
        width: 15,
        height: 24
      }
      this.markers = [startMarker, carMarker, endMarker]
    },
相关推荐
清风絮柳2 小时前
51. “闲转易”交易平台小程序(基于springboot&vue)
vue.js·spring boot·小程序·毕业设计·校园二手交易平台·二手交易小程序·闲转易交易系统
dr李四维2 小时前
vue生命周期、钩子以及跨域问题简介
前端·javascript·vue.js·websocket·跨域问题·vue生命周期·钩子函数
小程序照片合成4 小时前
uniapp微信小程序开发工具本地获取指定页面二维码
微信小程序·小程序·uniapp·二维码
教练 我想学编程4 小时前
学习记录706@微信小程序+springboot项目 真机测试 WebSocket错误: {errMsg: Invalid HTTP status.}连接不上
spring boot·学习·微信小程序
桂月二二5 小时前
Vue3状态管理深度解析:Pinia架构设计与性能优化实践
vue.js
程序猿看视界7 小时前
Uni-app页面信息与元素影响解析
uni-app·状态栏·安全区域·窗口信息·像素比
清晨細雨7 小时前
UniApp集成极光推送详细教程
android·ios·uni-app·极光推送
_未知_开摆7 小时前
uniapp APP端在线升级(简版)
开发语言·前端·javascript·vue.js·uni-app
喝拿铁写前端7 小时前
不同命名风格在 Vue 中后台项目中的使用分析
javascript·vue.js