获取当前坐标
wx.getLocation({
type: 'gcj02',
success: function (res) {
console.log("当前坐标信息:", res)
}
})
// 举个栗子验证一下distance方法的真实性和有效性
标记坐标 当点击标记点时触发导航功能
markertap: function (e) {
wx.openLocation({
latitude: 39.915378,
longitude: 116.403694,
scale: 18,
name: '天安门广场',
address:'北京市东城区长安街'
})
},
测量两坐标点距离
distance: function (la1, lo1, la2, lo2) {
var La1 = la1 * Math.PI / 180.0;
var La2 = la2 * Math.PI / 180.0;
var La3 = La1 - La2;
var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
s = s * 6378.137;
s = Math.round(s * 10000) / 10000;
s = s.toFixed(2);
return s;
}
使用
Page({
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that = this;
// 以北京故宫为例计算当前位置到其的距离,北京故宫坐标(116.403802, 39.915405)
wx.getLocation({
type: 'gcj02',
success: function (res) {
console.log("当前坐标信息:", res)
var distance = that.distance(res.latitude, res.longitude,39.918034,116.415192);
console.log("当前位置距离北京故宫:", distance, "千米")
}
})
// 举个栗子验证一下distance方法的真实性和有效性
// 北京故宫坐标(116.403802,39.915405),上海虹桥机场坐标(121.334421,31.200479),地图测距结果为1066.6±10
var testdistance=that.distance(39.915405, 116.403802, 31.200479, 121.334421);//注意经纬度别混淆
console.log("北京故宫-上海虹桥机场的距离为:",testdistance,"千米");//结果让人很满意
},
/**
* @desc 由经纬度计算两点之间的距离,la为latitude缩写,lo为longitude
* @param la1 第一个坐标点的纬度
* @param lo1 第一个坐标点的经度
* @param la2 第二个坐标点的纬度
* @param lo2 第二个坐标点的经度
* @return (int)s 返回距离(单位千米或公里)
* @tips 注意经度和纬度参数别传反了,一般经度为0~180、纬度为0~90
* 具体算法不做解释,有兴趣可以了解一下球面两点之间最短距离的计算方式
*/
distance: function (la1, lo1, la2, lo2) {
var La1 = la1 * Math.PI / 180.0;
var La2 = la2 * Math.PI / 180.0;
var La3 = La1 - La2;
var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
s = s * 6378.137;
s = Math.round(s * 10000) / 10000;
s = s.toFixed(2);
return s;
},
})
综合应用(测试指定坐标到当前位置的距离)
页面代码
<button type="primary" bindtap="queryAddress"> 读取当前地址坐标并查询地址 </button>
<button type="warn" bindtap="selAddress"> 选地址计算距离 </button>
JS代码
const WXAPI = require('apifm-wxapi')
Page({
data: {
latitude: undefined,
longitude: undefined
},
onLoad: function (options) {
},
onShow: function () {
},
queryAddress(){ // 读取当前定位坐标
const _this = this
wx.getLocation({
type: 'wgs84',
success(res) {
console.log(res)
_this.setData(res)
_this.mapQQAddress(res)
}
})
},
mapQQAddress(e){ // 坐标查地址
const location = e.latitude + ',' + e.longitude
WXAPI.mapQQAddress(location, 1).then(res => {
console.log('地址查看:', res)
if (res.code == 0) {
wx.showModal({
title: '成功',
content: res.data.result.address,
showCancel: false
})
}
})
},
selAddress(){ // 选择一个地址,读取坐标后计算距离
const _this = this
if (!this.data.latitude || !this.data.longitude) {
wx.showToast({
title: '请先读取当前地址',
icon: 'none'
})
return
}
wx.chooseLocation({
success: (e) => {
console.log(e)
WXAPI.mapDistance(_this.data.latitude, _this.data.longitude, e.latitude, e.longitude).then(res => {
console.log(res)
if (res.code == 0) {
wx.showModal({
title: '成功',
content: '距离:' + res.data + '公里',
showCancel: false
})
}
})
}
})
}
})