代码优化
This commit is contained in:
@@ -567,7 +567,7 @@ export default {
|
||||
})
|
||||
})
|
||||
},
|
||||
getBmapLocation: function(ak = 'r42BavbvCM0IA1eyo7bsD0aAZadiV4Q8') {
|
||||
getBmapLocation: function() {
|
||||
// #ifdef H5
|
||||
return this.getAmapLocationH5();
|
||||
// #endif
|
||||
@@ -764,50 +764,65 @@ export default {
|
||||
let cacheTime = uni.getStorageSync('location_cache_time');
|
||||
let now = new Date().getTime();
|
||||
if (cachedLocation && cacheTime && (now - cacheTime) < 30 * 60 * 1000) {
|
||||
resolve(cachedLocation);
|
||||
resolve(this.normalizeLocation(cachedLocation));
|
||||
return;
|
||||
}
|
||||
if (!navigator.geolocation) {
|
||||
that.showToast({ title: '您的浏览器不支持地理位置功能' });
|
||||
reject(new Error('Geolocation not supported'));
|
||||
that.fetchIpFromAmap().then((ipLoc)=>{
|
||||
let locationInfo = this.normalizeLocation({ lat: 0, lng: 0, address: '', province: ipLoc.province || '', city: ipLoc.city || '', district: '' });
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
}).catch(()=>{
|
||||
reject(new Error('Geolocation not supported'));
|
||||
});
|
||||
return;
|
||||
}
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
async (position) => {
|
||||
try {
|
||||
const { latitude, longitude } = position.coords;
|
||||
let { latitude, longitude } = position.coords;
|
||||
let gcj = this.wgs84ToGcj02(longitude, latitude);
|
||||
let useLng = gcj.lng, useLat = gcj.lat;
|
||||
let quotaExpire = uni.getStorageSync('amap_geocoder_quota_expire');
|
||||
let quotaExceeded = quotaExpire && (now < quotaExpire);
|
||||
if (quotaExceeded) {
|
||||
let locationInfo = { lat: latitude, lng: longitude, address: '', province: '', city: '', district: '' };
|
||||
let locationInfo = this.normalizeLocation({ lat: useLat, lng: useLng, address: '', province: '', city: '', district: '' });
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
return;
|
||||
}
|
||||
const response = await that.fetchAddressFromAmap(latitude, longitude);
|
||||
const response = await that.fetchAddressFromAmap(useLat, useLng);
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
lat: useLat,
|
||||
lng: useLng,
|
||||
address: response.address || '',
|
||||
province: response.province || '',
|
||||
city: response.city || '',
|
||||
district: response.district || ''
|
||||
};
|
||||
locationInfo = this.normalizeLocation(locationInfo);
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
} catch (error) {
|
||||
let locationInfo = { lat: position.coords.latitude, lng: position.coords.longitude, address: '', province: '', city: '', district: '' };
|
||||
let gcj = this.wgs84ToGcj02(position.coords.longitude, position.coords.latitude);
|
||||
let locationInfo = this.normalizeLocation({ lat: gcj.lat, lng: gcj.lng, address: '', province: '', city: '', district: '' });
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
console.error('获取位置失败:', error);
|
||||
that.showToast({ title: '获取位置失败,请检查浏览器位置权限设置' });
|
||||
reject(error);
|
||||
that.fetchIpFromAmap().then((ipLoc)=>{
|
||||
let locationInfo = this.normalizeLocation({ lat: 0, lng: 0, address: '', province: ipLoc.province || '', city: ipLoc.city || '', district: '' });
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
}).catch(()=>{
|
||||
reject(error);
|
||||
});
|
||||
},
|
||||
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 30 * 60 * 1000 }
|
||||
);
|
||||
@@ -848,11 +863,12 @@ export default {
|
||||
city: response.city || '',
|
||||
district: response.district || ''
|
||||
};
|
||||
locationInfo = this.normalizeLocation(locationInfo);
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
} catch (e) {
|
||||
let locationInfo = { lat: res.latitude, lng: res.longitude, address: '', province: '', city: '', district: '' };
|
||||
let locationInfo = this.normalizeLocation({ lat: res.latitude, lng: res.longitude, address: '', province: '', city: '', district: '' });
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
@@ -910,6 +926,54 @@ export default {
|
||||
});
|
||||
});
|
||||
},
|
||||
fetchIpFromAmap: function() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const key = '0806ce19c290f720e7699ab73e8b9088';
|
||||
uni.request({
|
||||
url: 'https://restapi.amap.com/v3/ip',
|
||||
data: { key },
|
||||
success: (res) => {
|
||||
if (res.data && res.data.status == '1') {
|
||||
resolve({ province: res.data.province || '', city: res.data.city || '' });
|
||||
} else {
|
||||
reject(new Error('AMap IP error'));
|
||||
}
|
||||
},
|
||||
fail: (error) => { reject(error); }
|
||||
});
|
||||
});
|
||||
},
|
||||
normalizeLocation: function(loc) {
|
||||
let lat = Number(loc.lat) || 0;
|
||||
let lng = Number(loc.lng) || 0;
|
||||
return {
|
||||
lat,
|
||||
lng,
|
||||
address: (loc.address || '').toString(),
|
||||
province: (loc.province || '').toString(),
|
||||
city: (loc.city || '').toString(),
|
||||
district: (loc.district || '').toString()
|
||||
};
|
||||
},
|
||||
wgs84ToGcj02: function(lng, lat) {
|
||||
function outOfChina(lng, lat) { return (lng < 72.004 || lng > 137.8347 || lat < 0.8293 || lat > 55.8271); }
|
||||
function transformLat(x, y) { let ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(y * Math.PI) + 40.0 * Math.sin(y / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (160.0 * Math.sin(y / 12.0 * Math.PI) + 320 * Math.sin(y * Math.PI / 30.0)) * 2.0 / 3.0; return ret; }
|
||||
function transformLon(x, y) { let ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x)); ret += (20.0 * Math.sin(6.0 * x * Math.PI) + 20.0 * Math.sin(2.0 * x * Math.PI)) * 2.0 / 3.0; ret += (20.0 * Math.sin(x * Math.PI) + 40.0 * Math.sin(x / 3.0 * Math.PI)) * 2.0 / 3.0; ret += (150.0 * Math.sin(x / 12.0 * Math.PI) + 300.0 * Math.sin(x / 30.0 * Math.PI)) * 2.0 / 3.0; return ret; }
|
||||
if (outOfChina(lng, lat)) { return { lng, lat }; }
|
||||
let a = 6378245.0;
|
||||
let ee = 0.00669342162296594323;
|
||||
let dLat = transformLat(lng - 105.0, lat - 35.0);
|
||||
let dLon = transformLon(lng - 105.0, lat - 35.0);
|
||||
let radLat = lat / 180.0 * Math.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) * Math.PI);
|
||||
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math.PI);
|
||||
let mgLat = lat + dLat;
|
||||
let mgLon = lng + dLon;
|
||||
return { lng: mgLon, lat: mgLat };
|
||||
},
|
||||
// 使用腾讯地图API获取详细地址信息
|
||||
fetchAddressFromTencent: function(lat, lng) {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -947,7 +1011,7 @@ export default {
|
||||
fail: (error) => {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user