微信小程序运单轨迹之路线规划-谷歌地图

这里用到谷歌地图api:

https://lbs.amap.com/api/wx/guide/route/route#demo 路线规划

https://lbs.amap.com/api/webservice/guide/api/georegeo 地理编码转换

知识点

  1. 多地址转换
  2. 路线规划完成后加载地图
- wxml地图代码
 <view class="map_box">
  <map id="navi_map" latitude="{{map.lat}}" longitude="{{map.lng}}" markers="{{map.markers}}" scale="8"  polyline="{{polyline}}" wx:if="{{map.hasMarkers}}">
  </map>
</view>

- js代码
var amapFile = require('../../utils/amap-wx.js') //地图api
var config = require('../../utils/config.js'); //两个地图的key
onLoad() {
    //loading代码块
    wx.showLoading({
      title: 'loading',
      icon: 'loading',
    })
     var that = this;
    var key = config.Config.key;
    var myAmapFun = new amapFile.AMapWX({ key: key });
     const trackList = (wx.getStorageSync('trackList')).reverse(); //接口缓存
     var  sendlocation = trackList[trackList.length -1].scanCity + trackList[trackList.length -1].scanCounty ;
      var  dispatchlocation = trackList[0].scanCity + trackList[0].scanCounty ;
      wx.request({
        url: 'https://restapi.amap.com/v3/geocode/geo', 
        data: {
          key:config.Config.key2,
          address:sendlocation+'|'+dispatchlocation,
          batch:true
        },
        method: 'GET',
        header: {
          'content-type': 'application/json;charset=utf-8' // 默认值
        },
        success(res) {
          const startlocation = res.data.geocodes[0].location;
          const endlocation = res.data.geocodes[1].location;
          //移动地图
          let _markers = [];
           _markers.push({
               iconPath: "../../image/mapicon_navi_s.png",
               id: 0,
               latitude: startlocation.split(',')[1],
               longitude: startlocation.split(',')[0],
               width: 23,
               height: 33,
          },{
            iconPath: "../../image/mapicon_navi_e.png",
               id: 0,
               latitude: endlocation.split(',')[1],
               longitude: endlocation.split(',')[0],
               width: 23,
               height: 33,
          });
          //绘制地图
          myAmapFun.getDrivingRoute({
            origin: startlocation,
            destination: endlocation,
            success: function (data) {
              wx.hideLoading();
              var points = [];
              if (data.paths && data.paths[0] && data.paths[0].steps) {
                var steps = data.paths[0].steps;
                for (var i = 0; i < steps.length; i++) {
                  var poLen = steps[i].polyline.split(';');
                  for (var j = 0; j < poLen.length; j++) {
                    points.push({
                      longitude: parseFloat(poLen[j].split(',')[0]),
                      latitude: parseFloat(poLen[j].split(',')[1])
                    })
                  }
                }
              }
              that.setData({
                'map.lat': startlocation.split(',')[1],
                'map.lng': startlocation.split(',')[0],
                'map.markers':_markers,
                'map.hasMarkers': true,
                polyline: [{
                  points: points,
                  color: "#0091ff",
                  width: 6,
                }]
              });  
            },
            fail: function (info) {
  
            }
          })
        }
      })
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容