微信小程序+laravel8 实现腾讯地图计算两点之间距离

 腾讯接口地址:WebService API | 腾讯位置服务

前端wxml代码:



前端js代码:

//获取目的地经纬度缓存起来
where() {// chooseLocationwx.chooseLocation({latitude: 0,success(res) {//目的地经度wx.setStorageSync('toLongitude',res.longitude )//目的地纬度wx.setStorageSync('toLatitude',res.latitude )  }})},
//通过api获取当前经纬度缓存起来here() {wx.getLocation({altitude: 'altitude',success(res) {//当前经度wx.setStorageSync('hereLongitude',res.longitude ) //  wx.getStorageSync('hereLongitude')//当前纬度wx.setStorageSync('hereLatitude',res.latitude ) wx.showToast({title: '获取成功',})}})},
//调用函数获取距离getdistance(){//将缓存起来的当期和目的地经纬度取出来发送到后端调用腾讯接口后返回距离给前端//所在经度let hereLongitude=wx.getStorageSync('hereLongitude');//所在纬度let hereLatitude=wx.getStorageSync('hereLatitude');//目的地经度let toLongitude=wx.getStorageSync('toLongitude');//目的地纬度let toLatitude=wx.getStorageSync('toLatitude');wx.request({url: 'http://www.exam.com/api/distance',data:{hereLongitude:hereLongitude,hereLatitude:hereLatitude,toLongitude:toLongitude,toLatitude:toLatitude},dataType:'json',success(res){let distance=res.data.data.result.rows[0].elements[0].distance;//打印距离}})},

后端代码: 

 public function distance(Request $request){$all=$request->all();//当前纬度$hereLatitude=$all['hereLatitude'];//经度$hereLongitude=$all['hereLongitude'];//目的地纬度$toLatitude=$all['toLatitude'];//目的地经度$toLongitude=$all['toLongitude'];$url="https://apis.map.qq.com/ws/distance/v1/matrix";//请求接口$key="";//申请的key$from=$hereLatitude.','.$hereLongitude;$to=$toLatitude.','.$toLongitude;$mode="driving";$url=$url.'?key='.$key.'&from='.$from.'&to='.$to.'&mode='.$mode;$result= json_decode(file_get_contents($url),true);return ['code'=>200,'mes'=>'接收成功','data'=>$result];}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部