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

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

1. 使用uniapp 提供的map组件作为承载页面
 <map
        id="store-order-map"
        :polyline="polyline"
        :markers="markers"
        :include-points="includePoints"
        :scale="10"
      ></map>
 data() {
    return {
      mapCtx: null
      }
   }
 mounted() {
    this.mapCtx = uni.createMapContext('store-order-map', this)
  },
获取路径方法
  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)
          }
        })
      })
    },
 生成路径方法
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
    },
创建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]
    },

http://www.kler.cn/a/551864.html

相关文章:

  • (学习总结25)Linux工具:vim 编辑器 和 gcc/g++ 编译器
  • 2024 年 6 月青少年软编等考 C 语言三级真题解析
  • 【linux】更换ollama的deepseek模型默认安装路径
  • 【Linux探索学习】第二十九弹——线程概念:Linux线程的基本概念与线程控制详解
  • 【ISO 14229-1:2023 UDS诊断(会话控制0x10服务)测试用例CAPL代码全解析④】
  • Qt 中使用 ffmpeg 获取采集卡数据录制视频
  • i++和++i的区别
  • 计算机网络(涵盖OSI,TCP/IP,交换机,路由器,局域网)
  • 从零到一实现微信小程序计划时钟:完整教程
  • C语言【基础篇】之函数——开启模块化开发的钥匙
  • Node.js和浏览器对JavaScript的支持区别
  • 基于STM32的智能环境监测系统
  • 完整实现CNN(Faster-RCNN)模型和Transformer(DETR)模型下遥感影像目标检测流程
  • 网站搭建基本流程
  • 华为 eNSP:MSTP
  • 使用linux脚本部署discuz博客(详细注释版)
  • 如何最优雅地部署 AWS Lambda?Lambda Version 与 Alias 的最佳实践
  • Sa-Token 根据官方文档简单实现登录认证的示例
  • 机器学习·最近邻方法(k-NN)
  • 【算法】快排