微信小程序-微信小程序限制当前位置和目的地的距离(签到)-小程序开发
1.1.获取当前位置经纬度
onLoad:function(options){varthat=this;campaign_id=campaign_idwx.getLocation({type:'wgs84',success:function(res){console.log(res)lat1=res.latitude,lng1=res.longitude}})}2.2.通过点击事件打开地图选择位置
site:function(){varthat=this;wx.chooseLocation({success:function(res){//name位置名称//address详细地址//latitude纬度,浮点数,范围为-90~90,负数表示南纬//longitude经度,浮点数,范围为-180~180,负数表示西经console.log(res);varsite_val=res.address;lat2=res.latitude;lng2=res.longitude;if(Number(juli(lat1,lng1,lat2,lng2))0.5){wx.showModal({title:'温馨提示',content:'位置不能离你大于500米',success:function(res){if(res.confirm){that.site()}elseif(res.cancel){console.log('用户点击取消')}}});}else{that.setData({site_val:site_val});}console.log('两点之间距离多少km:',juli(lat1,lng1,lat2,lng2));}})},3.3.计算两地之间的距离
//计算两地之间的距离functionjuli(lat1,lng1,lat2,lng2){console.log(lat1,lng1,lat2,lng2)varradLat1=lat1*Math.PI/180.0;varradLat2=lat2*Math.PI/180.0;vara=radLat1-radLat2;varb=lng1*Math.PI/180.0-lng2*Math.PI/180.0;vars=2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2)+Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));s=s*6378.137;s=Math.round(s*10000)/10000;returns}4.这里是计算起点到终点有多少米,你可以根据返回的米数来限制当前位置和目的地的距离
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。

