js根据经纬度计算两点距离
1.html
<button type="button" οnclick="distanceByLnglat(116.95400,39.95400,116.95300,39.95300);">
test me
</button>
2.js
需要引入
<script type="text/javascript" src="js/jquery.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js">
function distanceByLnglat(lng1, lat1, lng2, lat2) {
var radLat1 = Rad(lat1);
var radLat2 = Rad(lat2);
var a = radLat1 - radLat2;
var b = Rad(lng1) - Rad(lng2);
var s = 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 * 6378137.0; // 取WGS84标准参考椭球中的地球长半径(单位:m)
s = Math.round(s * 10000) / 10000;
alert(s);
// //下面为两点间空间距离(非球面体)
// var value= Math.pow(Math.pow(lng1-lng2,2)+Math.pow(lat1-lat2,2),1/2);
// alert(value);
}
function Rad(d) {
return d * Math.PI / 180.0;
}
// 7.2 获取当前地理位置
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '${appId}', // 必填,公众号的唯一标识
timestamp: '${ timestamp}', // 必填,生成签名的时间戳
nonceStr: '${ nonceStr}', // 必填,生成签名的随机串
signature: '${ signature}', // 必填,签名,见附录1
jsApiList: ['checkJsApi',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType', //网络状态接口
'openLocation', //使用微信内置地图查看地理位置接口
'getLocation' //获取地理位置接口
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.getLocation({
success: function(res) {
alert(JSON.stringify(res));
},
cancel: function(res) {
alert('用户拒绝授权获取地理位置');
}
});
//初始化jsapi接口 状态
wx.error(function(res) {
alert("调用微信jsapi返回的状态:" + res.errMsg);
});
效果: