通勤助手小程序使用了线路规划这一功能,借助腾讯地图的
WebService API中的路线规划服务和划线可实现这一功能。index.wxml文件如下,点击路线形成规划图。index.js文件如下
-
<map id="map" polyline="{{polyline}}" markers="{{markers}}" include-points="{{markers}}" style="width: 100%; height: 700rpx;" ></map>
-
-
<button bindtap="end">路线</button>
index.js文件如下
-
var coors
-
var that2
-
-
-
Page({
-
data: {
-
polyline: [],
-
markers: [{
-
iconPath: "mark.png",
-
latitude: 23.362490,
-
longitude: 116.715790,
-
width: 25,
-
height: 25
-
},
-
{
-
iconPath: "mark.png",
-
latitude: 23.37228,
-
longitude: 116.75498,
-
width: 25,
-
height: 25
-
}],
-
-
-
},
-
onReady: function () {
-
that2 = this
-
wx.request({
-
url: 'https://apis.map.qq.com/ws/direction/v1/driving/?from=23.362490,116.715790&to=23.37228,116.75498&output=json&callback=cb&key=22VBZ-REEK5-WVSI7-QKCOP-QPM6E-W7BPO',
-
-
success: function (res) {
-
-
-
coors = res.data.result.routes[0].polyline
-
-
for (var i = 2; i < coors.length; i++)
-
{ coors[i] = coors[i - 2] + coors[i] / 1000000 }
-
console.log(coors)
-
}
-
-
})
-
-
},
-
-
-
-
-
-
-
-
end: function () {
-
-
-
var b=[]
-
for (var i = 0; i < coors.length; i=i+2)
-
{
-
b[i / 2] = {
-
latitude: coors[i],longitude:coors[i+1]}
-
console.log(b[i/2])
-
}
-
console.log(b.length)
-
-
-
that2.setData({
-
polyline: [{
-
points: b,
-
color: "#99FF00",
-
width: 4,
-
dottedLine: false
-
}],
-
-
-
})
-
}
-
-
})
定义两点坐标并mark标注,通过api获取路径规划路线,返回的压缩后的经纬度,解压缩后出现成对的经纬度,再转换为polyline划线的格式。
-