微信网页jssdk开发文档获取地理位置接口文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#36
实现:
const wx = require('weixin-js-sdk')
const jsApiList = [ 'getLocation' ]
/**
* 获取定位
* @param {*} configData 接口获取
* @param {*} okCallBack 回调
*/
export const getLocation = function(configData, callback) {
wx.config({
debug: false,
appId: configData.appId,
timestamp: configData.timestamp,
nonceStr: configData.noncestr,
signature: configData.signature,
jsApiList: jsApiList
})
wx.ready(function() {
let lat1 = ''
let lng1 = ''
wx.getLocation({
type: 'wgs84',
success: function(res) {
lat1 = res.latitude
lng1 = res.longitude
callback && callback(lat1, lng1)
}
})
})
wx.error(function(res) {
console.log(res)
})
}
// 计算距离(单位:米)
export const getDistance = function(lat1, lng1, lat2, lng2) {
var EARTH_RADIUS = 6378137.0; //单位M
var PI = Math.PI;
function getRad(d) {
return d * PI / 180.0;
}
var radLat1 = getRad(lat1)
var radLat2 = getRad(lat2)
var a = radLat1 - radLat2
var b = getRad(lng1) - getRad(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 * EARTH_RADIUS
s = Math.round(s * 10000) / 10000.0
return s
}