美文网首页
微信小程序运单轨迹之路线规划-谷歌地图

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

作者: zsm_59a3 | 来源:发表于2019-04-30 11:27 被阅读0次
    这里用到谷歌地图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) {
      
                }
              })
            }
          })
    }
    

    相关文章

      网友评论

          本文标题:微信小程序运单轨迹之路线规划-谷歌地图

          本文链接:https://www.haomeiwen.com/subject/mtvmnqtx.html