小程序开发过程中获取用户当前地理位置,所以考虑基于用户当前经纬度,从而判断具体地理位置,具体代码如下:
fnGetlocation() {
let that = this;
uni.getLocation({
type: "wgs84", //默认为 wgs84 返回 gps 坐标
geocode: "true",
isHighAccuracy: true,
// accuracy: "best", // 精度值为20m
success: function (res) {
console.log("定位获取:", res);
const re = that.wgs84Togcj02(res.longitude,res.latitude)
console.log("当前平台: ",platform)
//that.getAreaCode(res.latitude, res.longitude);
if (platform == "mp-toutiao") {
console.log(res.city)
that.locationCityArea=res.city;
}else{
console.log(platform)
that.getAreaCode(re[1], re[0]);
}
},
fail(err) {
if (
err.errMsg ===
"getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化"
) {
uni.showToast({
title: "请勿频繁定位",
icon: "none",
});
}
if (err.errMsg === "getLocation:fail auth deny") {
// 未授权
uni.showToast({
title: "无法定位,请重新获取位置信息",
icon: "none",
});
authDenyCb && authDenyCb();
that.isLocated = false;
}
if (
err.errMsg ===
"getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF"
) {
uni.showModal({
content: "请开启手机定位服务",
showCancel: false,
});
}
console.log(err.errMsg)
},
});
},
//下面是基于QQ sdk基于经纬度获取当前位置信息
getAreaCode(latitude, longitude) {
let that=this;
console.log("get area info from lat "+latitude+", long "+longitude)
//this.openMap(longitude,latitude)
// this.latitude=latitude.toFixed(6)
// this.longitude=longitude.toFixed(6)
// 解析地址
QQMapWX.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude
},
success: function(res) {
console.log("解析地址成功"+ res);
// 省
let province = res.result.ad_info.province;
// 市
let city = res.result.ad_info.city;
let time = new Date()
//that.chengshi=city;
// console.log(province);
// console.log(city);
that.locationCityArea=city;
//console.log(this.locationCityArea)
//将位置写入storage
//that.setLocation(latitude.toFixed(6),longitude.toFixed(6),city,res.result.address,time.toLocaleString())
},
fail: function(res) {
uni.showToast({
title: '定位失败',
duration: 2000,
icon: "none"
})
},
complete: function(res) {
console.log(res);
}
})
},
wgs84Togcj02(lng, lat) {
if (this.out_of_china(lng, lat)) {
return [lng, lat]
}
//定义一些常量
//GCJ02 转换为 WGS84
var that = this;
const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
const PI = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
let dlat = that.transformlat(lng - 105.0, lat - 35.0);
let dlng = that.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);
var mglat = lat + dlat;
var mglng = lng + dlng;
return [mglng, mglat]
//return [lng * 2 - mglng, lat * 2 - mglat]
},
out_of_china(lng, lat) {
return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
},
transformlat(lng, lat) {
const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
const PI = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
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
},
transformlng(lng, lat) {
const x_PI = 3.14159265358979324 * 3000.0 / 180.0;
const PI = 3.1415926535897932384626;
const a = 6378245.0;
const ee = 0.00669342162296594323;
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
}