微信小程序绘制线路以及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]
    })

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
微信小程序可以使用openlayers,但需要注意的是,openlayers是一个基于WebGL的库,而微信小程序不支持WebGL,因此需要使用openlayers提供的Canvas渲染方式。具体实现方法如下: 1. 引入openlayers库文件 在小程序的wxml文件中,使用canvas组件来绘制openlayers图层,可以在js文件中引入openlayers库文件,如下所示: ```javascript import ol from 'openlayers/dist/ol-debug'; ``` 2. 创建地图容器 在wxml文件中,添加canvas组件,并设置id和样式,如下所示: ```html <canvas id="map" style="width: 100%; height: 100%;"></canvas> ``` 3. 初始化地图 在js文件中,初始化地图容器和地图图层,如下所示: ```javascript let map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([120.15, 30.28]), zoom: 10 }) }); ``` 4. 绘制图形 使用openlayers提供的绘制工具,可以在地图上绘制点、线、面等图形。如下所示,绘制一个点: ```javascript let marker = new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([120.15, 30.28])) }); let markerLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [marker] }), style: new ol.style.Style({ image: new ol.style.Icon({ src: '/images/marker.png', size: [32, 32], anchor: [0.5, 1] }) }) }); map.addLayer(markerLayer); ``` 5. 注意事项 在使用openlayers时,需要注意以下几点: - openlayers库文件较大,需要进行压缩和优化; - 小程序的Canvas组件的绘图能力有限,可能会影响openlayers的性能表现; - openlayers不支持小程序的事件机制,需要自行处理用户交互事件; - openlayers的地图数据需要通过网络加载,可能会影响地图的加载速度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Android毕业设计源码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值