欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信小程序 实现路线规划

程序员文章站 2024-02-11 17:02:58
...

微信小程序 实现路线规划

route(fromlatlng, tolatlng) {// 路线规划
var that = this;
  let routeparam = {
    from: fromlatlng,//起点经纬度,小数点后不超过6位,40.056878,116.30815
    to: tolatlng,//终点经纬度,小数点后不超过6位,
    key: 'DASC431-Q321EO-TNEW5-12REQ-Q231I6-FSFDS21',//腾讯地图开放平台申请key
  };
  uni.request({
    url: 'https://apis.map.qq.com/ws/direction/v1/driving',
    data: routeparam,
    header: {
      "content-type": "application/json"
    },
    method: 'GET',
    success(ret) {
		if(ret.data.status==0){
			// console.log(ret.data.result.routes[0].polyline)
			//从结果中取出路线坐标串
			var coors = ret.data.result.routes[0].polyline, pl = [];
			//坐标解压(返回的点串坐标,通过前向差分进行压缩,因此需要解压)
			var kr = 1000000;
			for (var i = 2; i < coors.length; i++) {
			  coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
			}
			//将解压后的坐标生成LatLng数组
			for (var i = 0; i < coors.length; i += 2) {
			  pl.push({
				  latitude:coors[i],
				  longitude:coors[i+1]
			  });
			}
			let ps = {
				points: pl,
				dottedLine: false,
				width: 5,
				color: "#2B97FD"
			}
			that.polyline = that.polyline.concat(ps);//绘制轨迹线
		}
    },
    fail(data) {
      fail(data);
    }
  });
},