uni-app实现轨迹回放

本文展示了如何在uni-app中配置地图key并实现轨迹回放功能。页面包含地图组件和控制按钮,数据绑定包括地图坐标、标记点和路线。主要逻辑在于onReady和start方法,利用uni.createMapContext进行地图操作,translateMarker方法实现标记点的动画移动,完成轨迹回放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果展示

ezgif-2-44c0749dd2.gif

配置地图key

在这里插入图片描述

页面结构

页面结构主要包括一个地图组件和一个按钮用来控制轨迹回放开始:

<view class="container">
  <map id='map' :latitude="latitude" :longitude="longitude" :markers="covers"
       :style="{ width: '100%', height: mapHeight + 'px' }" :scale="13" :polyline="polyline">
  </map>

  <view class="btnBox">
    <button :disabled="isDisabled" @click="start" class="cu-btn bg-blue round shadow lg">轨迹回放</button>
  </view>
</view>

数据绑定

data() {
  return {
    map: null,
    isDisabled: false,
    isStart: false,
    playIndex: 1,
    latitude: 34.263734,
    longitude: 108.934843,
    // 标记点(小车),详细见:https://uniapp.dcloud.net.cn/component/map.html#marker
    covers: [{
      id: 1,
      width: 42,
      height: 47,
      // 使用的小车图片默认车头方向朝左,然后因为 h5 中是负值为逆时针旋转,而微信小程序等中是正值逆时针旋转,所以这里使用了条件编译,仅在 h5 模式下使用负值
      // #ifndef H5
      rotate: -90,
      // #endif
      // #ifdef H5
      rotate: 90,
      // #endif
      latitude: 34.259428,
      longitude: 108.947040,
      iconPath: 'http://cdn.zhoukaiwen.com/car.png',
      callout: {
        content: "豫S·SB250", // 车牌信息
        display: "ALWAYS",
        fontWeight: "bold",
        color: "#5A7BEE", //文本颜色
        fontSize: "12px",
        bgColor: "#ffffff", //背景色
        padding: 5, //文本边缘留白
        textAlign: "center",
      },
      anchor: {
        x: 0.5,
        y: 0.5,
      },
    }],

    // 路线
    polyline: [],

    // 坐标数据,一般都是后端返回
    coordinate: [{
      latitude: 34.259428,
      longitude: 108.947040,
      problem: false,
    },{
      latitude: 34.252918,
      longitude: 108.946963,
      problem: false,
    },{
      latitude: 34.252408,
      longitude: 108.946240,
      problem: false,
    },{
      latitude: 34.249286,
      longitude: 108.946184,
      problem: false,
    },{
      latitude: 34.248670,
      longitude: 108.946640,
      problem: false,
    },{
      latitude: 34.248129,
      longitude: 108.946826,
      problem: false,
    },{
      latitude: 34.243537,
      longitude: 108.946816,
      problem: true,
    },{
      latitude: 34.243478,
      longitude: 108.939003,
      problem: true,
    },{
      latitude: 34.241218,
      longitude: 108.939027,
      problem: true,
    },{
      latitude: 34.241192,
      longitude: 108.934802,
      problem: true,
    },{
      latitude: 34.241182,
      longitude: 108.932235,
      problem: true,
    },{
      latitude: 34.247227,
      longitude: 108.932311,
      problem: true,
    },{
      latitude: 34.250833,
      longitude: 108.932352,
      problem: true,
    },{
      latitude: 34.250877,
      longitude: 108.931756,
      problem: true,
    }, {
      latitude: 34.250944,
      longitude: 108.931576,
      problem: true,
    },{
      latitude: 34.250834,
      longitude: 108.929662,
      problem: true,
    },{
      latitude: 34.250924,
      longitude: 108.926015,
      problem: true,
    },{
      latitude: 34.250802,
      longitude: 108.910121,
      problem: true,
    },{
      latitude: 34.269718,
      longitude: 108.909921,
      problem: true,
    },{
      latitude: 34.269221,
      longitude: 108.922366,
      problem: false,
    },{
      latitude: 34.274531,
      longitude: 108.922388,
      problem: false,
    },{
      latitude: 34.276201,
      longitude: 108.923433,
      problem: false,
    },{
      latitude: 34.276559,
      longitude: 108.924004,
      problem: false,
    },{
      latitude: 34.276785,
      longitude: 108.945855,
      problem: false,
    }]
  }
}

主要逻辑

onReady() {
  // 创建map对象
  this.map = uni.createMapContext('map');
},
mounted() {
  // 读取路径数据
  this.polyline = [{
    points: this.coordinate,
    color: '#025ADD',
    width: 4,
    dottedLine: false,
  }];
},
methods: {
  start() {
    this.isStart = true; // 开始回放
    this.isDisabled = true; // 禁用按钮
    let data = this.coordinate;
    let len = data.length;
    let datai = data[this.playIndex]; // 读取下一个要到达的点
    let _this = this; // 暂存 this

    // 参考:https://uniapp.dcloud.net.cn/api/location/map.html#createmapcontext
    _this.map.translateMarker({
      markerId: 1,
      autoRotate: true,
      destination: {
        longitude: datai.longitude, // 车辆即将移动到的下一个点的经度
        latitude: datai.latitude, // 车辆即将移动到的下一个点的纬度
      },
      duration: 700,
      // 动画结束
      animationEnd: function() {
        _this.playIndex++; 
        if (_this.playIndex < len) {
          _this.start();
        } else {
          uni.showToast({
            title: '回放完成',
            duration: 1400,
            icon: 'none'
          });
          _this.playIndex = 0;
          _this.isStart = false;
          _this.isDisabled = false;
        }
      },
      fail(e) {
        // 轨迹回放失败回调
      },
    });
  }
}

注意

本文章代码 90% 以上都是参考开源项目 前端铺子,如帮助到你,可以去大佬项目 gitee地址 点个 star

实现车辆轨迹回放,可以使用uniapp的map组件。以下是步骤: 1.引入map组件: 在页面的vue文件里,先引入map组件。如下: ``` <template> <view> <map :show-location="true" :markers="markers"></map> </view> </template> <script> import {uniMap} from '@dcloudio/uni-ui'; export default { components: { uniMap }, data(){ return { markers: [] } } } </script> ``` 2.设置地图样式: 在页面的style里,设置map组件的高度和宽度 ``` <style> map { width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 999; } </style> ``` 3.加载地图: 在mounted钩子函数里调用uniMap的createMap方法,创建地图 ``` mounted(){ this.createMap(); }, methods: { createMap(){ let mapCtx = uniMap.createMapContext('myMap', { showLocation: true }) mapCtx.moveToLocation(); } } ``` 4.显示轨迹: 使用setMarkers方法,将要显示的轨迹点添加到地图上 ``` let markers = [ { id: 1, longitude: 113.324520, latitude: 23.10229, iconPath: '/static/img/car.png', width: 50, height: 50, callout: { content: '起点' } }, { id: 2, longitude: 113.35938, latitude: 23.09211, iconPath: '/static/img/car.png', width: 50, height: 50, callout: { content: '终点' } }, { id: 3, longitude: 113.331441, latitude: 23.117706, iconPath: '/static/img/car.png', width: 50, height: 50, callout: { content: '中间点' } } ] this.markers = markers; ``` 5.轨迹回放: 使用定时器和moveToLocation方法,按照轨迹点顺序将地图移动到对应的位置 ``` play(){ let mapCtx = uniMap.createMapContext('myMap', { showLocation: true }) let i = 0; let len = markers.length; let timer = setInterval(() => { if(i >= len - 1){ clearInterval(timer); return; } i++; mapCtx.moveToLocation({ longitude: markers[i].longitude, latitude: markers[i].latitude }) }, 1000) } ``` 以上就是实现车辆轨迹回放的步骤。需要注意的是,地图的显示需要设置高度和宽度,因为地图是一个fixed定位的元素。另外,回放轨迹时需要使用定时器来控制地图移动的速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鹏北海-RemHusband

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值