微信小程序绘制线路以及marker轨迹移动

目录

效果图:

一、布局文件

二、js文件

三、代码说明


效果图:

一、布局文件

<map class="mapUI" id="myMap" markers="{{markers}}" latitude="{{latitude}}" longitude="{{longitude}}" polyline="{{polyline}}" include-points='{{points}}' scale="16"></map>

二、js文件

export { }
const appData = getApp();
var pointinfor = []
var thatInfor

Page({

  data: {
    gps: ["108.939087,34.250638", "108.939087,34.240917", "108.932135,34.241095", "108.932006,34.235702", "108.932049,34.230291", "108.931985,34.228233", "108.931942,34.222778", "108.931942,34.218058", "108.939151,34.218236", "108.939055,34.214536", "108.943153,34.214536", "108.946587,34.214478"],
    searchData: [],
    lineData: [],
    polyline: [],
    markers: [],
    points: [],
    orgId: "",
    token: "",
    codeModel: {},
    choicePersonInfor: {},
    companyData: [],

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    this.mapCtx = wx.createMapContext('myMap')
    this.mapCtx.moveToLocation()
    var pointinfor = [];
    for (var index = 0; index < this.data.gps.length; index++) {
      pointinfor.push({
        latitude: this.data.gps[index].split(",")[1],
        longitude: this.data.gps[index].split(",")[0],
      })
    }

    let mapAddress = [];
    mapAddress.push({
      latitude: this.data.gps[0].split(",")[1],
      longitude: this.data.gps[0].split(",")[0],
      id: 20,
      iconPath: '../image/mocar.png',
      width: '40px',
      height: '60px',
      anchor: {
        x: 0.5,
        y: 0.4
      }
    });
    mapAddress.push({
      id: 1001,
      latitude: this.data.gps[0].split(",")[1],
      longitude: this.data.gps[0].split(",")[0],
      name: "起点",
      iconPath: '../image/icon_start.png',
      content: "111",
      width: '30px',
      height: '45px',
    });

    mapAddress.push({
      id: 9001,
      latitude: this.data.gps[this.data.gps.length - 1].split(",")[1],
      longitude: this.data.gps[this.data.gps.length - 1].split(",")[0],
      name: "终点",
      iconPath: '../image/icon_end.png',
      content: "111",
      width: '30px',
      height: '45px',
    });


    mapAddress.push({
      id: 1011,
      latitude: this.data.gps[3].split(",")[1],
      longitude: this.data.gps[3].split(",")[0],
      name: "幸福小区接送点",
      iconPath: '/pages/image/markerimage.png',
      width: '34px',
      height: '34px',
      content: "111",
      callout: {
        content: "幸福小区接送点",
        padding: 5,
        fontSize: 12,
        textAlign: 'center',
        display: 'ALWAYS',
        borderRadius: 5,
        borderWidth: 1,
        bgColor: '#ffffff',
        borderColor: "#c3c3c3"
      },

    });

    mapAddress.push({
      id: 1012,
      latitude: this.data.gps[6].split(",")[1],
      longitude: this.data.gps[6].split(",")[0],
      name: "幸福小区接送点",
      iconPath: '/pages/image/markerimage.png',
      width: '34px',
      height: '34px',
      content: "111",
      callout: {
        content: "雁塔小区接送点",
        padding: 5,
        fontSize: 12,
        textAlign: 'center',
        display: 'ALWAYS',
        borderRadius: 5,
        borderWidth: 1,
        bgColor: '#ffffff',
        borderColor: "#c3c3c3"
      },
    });

    this.setData({
      markers: mapAddress,
      polyline: [{
        points: pointinfor,
        color: '#09B988',
        width: 5,
        dottedLine: false,
        arrowLine: true,
        borderColor: "#09B988",
        borderWidth: 1,
      }],
    })

    var mapCtx = wx.createMapContext("myMap");

    mapCtx.includePoints({
      points: pointinfor,
      padding: [50]
    })

    mapCtx.moveAlong({
      markerId: 20,
      autoRotate: true,
      path: pointinfor,
      duration: 50000,
    })
  }, 
})

三、代码说明

1:首先需要给绘制的线路设置坐标数据信息

2:对起点和终点设置两个marker

3:设置小车轨迹的移动

 mapCtx.moveAlong({
      markerId: 20,
      autoRotate: true,
      path: pointinfor,
      duration: 50000,
    })

4:设置线路与地图边距的距离代码

   mapCtx.includePoints({
      points: pointinfor,
      padding: [50]
    })

### 微信小程序实现跑步轨迹功能 要在微信小程序中实现跑步轨迹功能,可以通过使用微信小程序内置的地图组件以及相关的 API 来完成。以下是关于该功能的具体说明: #### 1. 使用地图组件和定时器实现轨迹移动效果 为了实现在地图上动态展示用户的跑步轨迹,可以利用微信小程序提供的 `map` 组件,并结合 JavaScript 的定时器技术模拟轨迹的连续变化[^1]。 ```html <view> <map id="runningMap" longitude="{{longitude}}" latitude="{{latitude}}" scale="18" show-location bindregionchange="bindRegionChange"> <cover-view wx:for="{{pathPoints}}" wx:key="index"> <cover-image src="/images/marker.png" style="width: 20px; height: 20px;" data-index="{{index}}" longitude="{{item.longitude}}" latitude="{{item.latitude}}"></cover-image> </cover-view> </map> </view> ``` 上述代码片段定义了一个地图视图,在其中通过循环渲染多个标记点来表示跑步轨迹的位置节点。 #### 2. 跑步轨迹的核心逻辑 在后台处理方面,需要不断更新用户当前位置并将这些坐标保存到数组中用于后续绘制。具体来说,这涉及以下几个部分: - **获取当前地理位置**:借助 `wx.getLocation()` 方法定期读取设备 GPS 数据。 - **存储路径点集合**:每次成功取得新位置后将其加入全局变量维护的一系列地点列表里。 - **刷新界面状态**:每当有新的地理信息被采集进来时触发重新渲染操作以反映最新情况。 对于实际开发过程中的注意事项还包括性能优化措施比如减少不必要的 DOM 操作频率等[^4]。 #### 3. 集成健康数据增强体验感 除了单纯记录跑动线路外还可以进一步扩展应用范围至健康管理领域——例如引入每日行走总步数统计图表等功能模块提升整体交互质量][^[^23]。这里提到的方法主要是依靠调用微信开放平台所提供的接口权限去访问个人隐私敏感资料(如计步详情),然后再经过适当加工呈现给最终使用者查看分析结果。 ```javascript // 示例伪代码演示如何请求并解析 WeRun 加密后的原始数据包内容 Page({ onLoad() { this.getUserProfile(); }, getUserProfile(){ wx.login({ success(res){ const code=res.code; // Step A - Exchange Code For Session Key & OpenID On Server Side... }}); //Step B - Fetch Encrypted Data Package From Client Applet End Point ... } }); ``` 以上脚本段落仅作概念验证用途,请务必遵循官方文档指导安全合规地执行相应流程步骤[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Android毕业设计源码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值