1、首先要弄清楚后端傳過來的經(jīng)緯度是屬于哪一類,一共有三大類,分別是:WGS84,GCJ02,BD09
WGS84:為一種大地坐標(biāo)系,也是目前廣泛使用的GPS全球衛(wèi)星定位系統(tǒng)使用的坐標(biāo)系。
GCJ02:又稱火星坐標(biāo)系,是由中國(guó)國(guó)家測(cè)繪局制定的地理坐標(biāo)系統(tǒng),是由WGS84加密后得到的坐標(biāo)系。
BD09:為百度坐標(biāo)系,在GCJ02坐標(biāo)系基礎(chǔ)上再次加密。其中bd09ll表示百度經(jīng)緯度坐標(biāo),bd09mc表示百度墨卡托米制坐標(biāo)。
2、百度、高德、谷歌使用對(duì)應(yīng)的類型
百度地圖——BD09
高德地圖——GCJ02
谷歌地圖——GCJ02
3、然后就是用戶獲取的經(jīng)緯度位置,uniapp有提供的方法uni.getLocation(),使用方法如下,默認(rèn)的是wgs84類型
uni.getLocation({
type: 'wgs84',
success: function (res) {
console.log('當(dāng)前位置的經(jīng)度:' + res.longitude);
console.log('當(dāng)前位置的緯度:' + res.latitude);
}
});
4、然后要知道后端返回的經(jīng)緯度是高德還是百度,亦或是谷歌,將獲取到用戶的經(jīng)緯度進(jìn)行轉(zhuǎn)換,轉(zhuǎn)換的方法如下:
/**
*
* 提供了百度坐標(biāo)(BD09)、國(guó)測(cè)局坐標(biāo)(火星坐標(biāo),GCJ02)、和WGS84坐標(biāo)系之間的轉(zhuǎn)換
*/
// UMD魔法代碼
// if the module has no dependencies, the above pattern can be simplified to
/* eslint-disable */
let coordtransform = function () {
// 定義一些常量
let x_PI = 3.14159265358979324 * 3000.0 / 180.0;
let PI = 3.1415926535897932384626;
let a = 6378245.0;
let ee = 0.00669342162296594323;
/**
* 百度坐標(biāo)系 (BD-09) 與 火星坐標(biāo)系 (GCJ-02)的轉(zhuǎn)換
* 即 百度 轉(zhuǎn) 谷歌、高德
* @param bd_lon
* @param bd_lat
* @returns {*[]}
*/
let bd09togcj02 = function bd09togcj02(bd_lon, bd_lat) {
bd_lon = +bd_lon;
bd_lat = +bd_lat;
let x = bd_lon - 0.0065;
let y = bd_lat - 0.006;
let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_PI);
let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_PI);
let gg_lng = z * Math.cos(theta);
let gg_lat = z * Math.sin(theta);
return [gg_lng, gg_lat]
};
/**
* 火星坐標(biāo)系 (GCJ-02) 與百度坐標(biāo)系 (BD-09) 的轉(zhuǎn)換
* 即谷歌、高德 轉(zhuǎn) 百度
* @param lng
* @param lat
* @returns {*[]}
*/
let gcj02tobd09 = function gcj02tobd09(lng, lat) {
lat = +lat;
lng = +lng;
let z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
let theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
let bd_lng = z * Math.cos(theta) + 0.0065;
let bd_lat = z * Math.sin(theta) + 0.006;
return [bd_lng, bd_lat]
};
/**
* WGS84轉(zhuǎn)GCj02
* @param lng
* @param lat
* @returns {*[]}
*/
let wgs84togcj02 = function wgs84togcj02(lng, lat) {
lat = +lat;
lng = +lng;
if (out_of_china(lng, lat)) {
return [lng, lat]
} else {
let dlat = transformlat(lng - 105.0, lat - 35.0);
let dlng = transformlng(lng - 105.0, lat - 35.0);
let radlat = lat / 180.0 * PI;
let magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
let sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
let mglat = lat + dlat;
let mglng = lng + dlng;
return [mglng, mglat]
}
};
/**
* GCJ02 轉(zhuǎn)換為 WGS84
* @param lng
* @param lat
* @returns {*[]}
*/
let gcj02towgs84 = function gcj02towgs84(lng, lat) {
lat = +lat;
lng = +lng;
if (out_of_china(lng, lat)) {
return [lng, lat]
} else {
let dlat = transformlat(lng - 105.0, lat - 35.0);
let dlng = transformlng(lng - 105.0, lat - 35.0);
let radlat = lat / 180.0 * PI;
let magic = Math.sin(radlat);
magic = 1 - ee * magic * magic;
let sqrtmagic = Math.sqrt(magic);
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
let mglat = lat + dlat;
let mglng = lng + dlng;
return [lng * 2 - mglng, lat * 2 - mglat]
}
};
let transformlat = function transformlat(lng, lat) {
lat = +lat;
lng = +lng;
let ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
return ret
};
let transformlng = function transformlng(lng, lat) {
lat = +lat;
lng = +lng;
let ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
return ret
};
/**
* 判斷是否在國(guó)內(nèi),不在國(guó)內(nèi)則不做偏移
* @param lng
* @param lat
* @returns {boolean}
*/
let out_of_china = function out_of_china(lng, lat) {
lat = +lat;
lng = +lng;
// 緯度3.86~53.55,經(jīng)度73.66~135.05
return !(lng > 73.66 && lng < 135.05 && lat > 3.86 && lat < 53.55);
};
return {
bd09togcj02: bd09togcj02,
gcj02tobd09: gcj02tobd09,
wgs84togcj02: wgs84togcj02,
gcj02towgs84: gcj02towgs84
}
}();
export default coordtransform
4、計(jì)算公式,目前市面上計(jì)算兩點(diǎn)之間距離的基本都是基于余弦球面定律,該定律使用三角函數(shù)來測(cè)量地球的曲率,以準(zhǔn)確測(cè)量地球上的距離。文章來源:http://www.zghlxwxcb.cn/news/detail-610953.html
// 經(jīng)緯度轉(zhuǎn)換成三角函數(shù)中度分表形式。
rad(d) {
return d * Math.PI / 180.0;
},
// 根據(jù)經(jīng)緯度計(jì)算距離,參數(shù)分別為第一點(diǎn)的緯度,經(jīng)度;第二點(diǎn)的緯度,經(jīng)度
getDistance(lat1, lng1, lat2, lng2) {
let radLat1 = this.rad(lat1);
let radLat2 = this.rad(lat2);
let a = radLat1 - radLat2;
let b = this.rad(lng1) - this.rad(lng2);
let 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 * 6378.137; // EARTH_RADIUS;
s = Math.round(s * 10000) / 10000; //輸出為公里
let distance = s;
let distance_str = "";
parseInt(distance) >= 1 ? distance_str = distance.toFixed(1) + "km" : distance_str = distance * 1000 + "m"
return [s,distance_str];
},
5、我在項(xiàng)目中的例子:文章來源地址http://www.zghlxwxcb.cn/news/detail-610953.html
data(){
// 獲取距離所用參數(shù)
latitude: 0, // 緯度
longitude: 0, // 經(jīng)度
}
// 獲取定位
getAddressInfo(){
let that = this
// 獲取用戶是否開啟 授權(quán)獲取當(dāng)前的地理位置、速度的權(quán)限。
uni.getSetting({
success (res) {
console.log(res)
// 如果沒有授權(quán)
if (!res.authSetting['scope.userLocation']) {
// 則拉起授權(quán)窗口
uni.authorize({
scope: 'scope.userLocation',
success () {
//點(diǎn)擊允許后--就一直會(huì)進(jìn)入成功授權(quán)的回調(diào) 就可以使用獲取的方法了
uni.getLocation({
type: 'wgs84',
success: function (res) {
let a = coordtransform.wgs84togcj02(res.longitude, res.latitude)
that.latitude = a[1]
that.longitude = a[0]
}, fail (error) {
that.$util.showMessage('當(dāng)前位置獲取失敗,將不顯示相關(guān)機(jī)構(gòu)到此的距離', 1500)
}
})
},
fail (error) {
uni.showModal({
content: '當(dāng)前位置獲取失敗,將不顯示相關(guān)機(jī)構(gòu)到此的距離',
cancelText: '不授權(quán)',
cancelColor: '#999999',
confirmText: '授權(quán)',
confirmColor: '#f94218',
success (res) {
console.log(res)
if (res.confirm) {
// 選擇彈框內(nèi)授權(quán)
uni.openSetting({
success (res) {
uni.getLocation({
type: 'wgs84',
success: function (res) {
let a = coordtransform.wgs84togcj02(res.longitude, res.latitude)
that.latitude = a[1]
that.longitude = a[0]
}, fail (error) {
that.$util.showMessage('獲取地址失敗,請(qǐng)檢查手機(jī)是否打開定位功能', 1500)
}
})
}
})
}
}
})
return
}
})
} else {
// 有權(quán)限則直接獲取
uni.getLocation({
type: 'wgs84',
success: function (res) {
let a = coordtransform.wgs84togcj02(res.longitude, res.latitude)
that.latitude = a[1]
that.longitude = a[0]
}, fail (error) {
that.$util.showMessage('獲取地址失敗,請(qǐng)檢查手機(jī)是否打開定位功能', 1500)
}
})
}
}
})
},
// 通過調(diào)用接口,拿到數(shù)據(jù)進(jìn)行計(jì)算(我這里計(jì)算后還進(jìn)行了排序,所以多了一步)
if(that.latitude != 0 && that.longitude != 0){
res.storeList.forEach(item => {
item.distance = that.getDistance(that.latitude,that.longitude,item.storeLat,item.storeLng)[1]
item.distanceFloat = that.getDistance(that.latitude,that.longitude,item.storeLat,item.storeLng)[0]
})
let temp = 0
for (let i = 0; i <= res.storeList.length - 1; i++) {
for (let j = 0; j < res.storeList.length - i - 1; j++) {
if (parseFloat(res.storeList[j].distanceFloat) > parseFloat(res.storeList[j + 1].distanceFloat)) {
temp = res.storeList[j]
res.storeList[j] = res.storeList[j + 1]
res.storeList[j + 1] = temp
}
}
}
}
到了這里,關(guān)于uniapp微信小程序,根據(jù)用戶當(dāng)前位置計(jì)算用戶到附近機(jī)構(gòu)/商店的距離的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!