优化代码
This commit is contained in:
@@ -567,146 +567,12 @@ export default {
|
||||
})
|
||||
})
|
||||
},
|
||||
//百度地图获取定位
|
||||
getBmapLocation: function(ak = 'GoI7BxLpfvBEyf1TcMXCloi99Vov7flZ') {
|
||||
getBmapLocation: function(ak = 'r42BavbvCM0IA1eyo7bsD0aAZadiV4Q8') {
|
||||
// #ifdef H5
|
||||
// H5环境下使用腾讯地图API
|
||||
return this.getTencentLocation();
|
||||
return this.getAmapLocationH5();
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
// 非H5环境使用百度地图API
|
||||
//定位
|
||||
let that = this;
|
||||
let bmap = require('./bmap-wx.min.js');
|
||||
let BMap = new bmap.BMapWX({
|
||||
ak
|
||||
});
|
||||
return new Promise((resove, reject) => {
|
||||
BMap.regeocoding({
|
||||
success: function(data) {
|
||||
let addressInfo = data.originalData.result;
|
||||
let {
|
||||
lat,
|
||||
lng
|
||||
} = addressInfo.location;
|
||||
let {
|
||||
formatted_address: address,
|
||||
addressComponent
|
||||
} = addressInfo;
|
||||
let {
|
||||
province,
|
||||
city,
|
||||
district
|
||||
} = addressComponent
|
||||
//只返回需要的数据
|
||||
let locationInfo = {
|
||||
lat,
|
||||
lng,
|
||||
address,
|
||||
province,
|
||||
city,
|
||||
district,
|
||||
}
|
||||
// console.log(locationInfo, "====util locationInfo");
|
||||
//成功回调
|
||||
resove(locationInfo)
|
||||
},
|
||||
fail: function(res) {
|
||||
//失败返回默认的数据
|
||||
let locationInfo = {
|
||||
name: '',
|
||||
latitude: 0,
|
||||
longitude: 0,
|
||||
address: '',
|
||||
city: ''
|
||||
}
|
||||
resove(locationInfo)
|
||||
that.hideAll()
|
||||
//失败后的提示
|
||||
let errMsg = res.errMsg;
|
||||
if (errMsg.includes("domain")) {
|
||||
uni.showModal({
|
||||
title: "获取定位失败",
|
||||
content: `请在小程序公众平台添加百度域名api.map.baidu.com`,
|
||||
showCancel: false
|
||||
})
|
||||
return;
|
||||
}
|
||||
if (errMsg.includes("Referer")) {
|
||||
uni.showModal({
|
||||
title: "获取定位失败",
|
||||
content: `登录百度开放平台给ak添加白名单`,
|
||||
showCancel: false
|
||||
})
|
||||
return;
|
||||
}
|
||||
// uni.showModal({
|
||||
// title: "获取定位失败",
|
||||
// content: "请检查手机是否开启定位功能",
|
||||
// showCancel: false
|
||||
// })
|
||||
|
||||
uni.showModal({
|
||||
title: "地理位置授权",
|
||||
content: "为了更好的为您服务,请开启您手机中的定位授权",
|
||||
confirmText: "去授权",
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.openSetting({
|
||||
success(result) {
|
||||
if (result.authSetting[
|
||||
`scope.userLocation`]) {
|
||||
resove(true)
|
||||
} else {
|
||||
reject()
|
||||
}
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
// H5环境下提示用户手动在浏览器设置中开启位置权限
|
||||
that.showToast({
|
||||
title: '请在浏览器设置中手动开启位置权限'
|
||||
});
|
||||
reject(new Error('Location permission denied'));
|
||||
// #endif
|
||||
} else {
|
||||
// #ifdef MP-WEIXIN
|
||||
uni.showModal({
|
||||
title: "提示",
|
||||
content: "您取消了授权,是否重新设置【位置信息】权限",
|
||||
confirmText: "去授权",
|
||||
success(res) {
|
||||
if (!res.confirm) return
|
||||
uni.openSetting({
|
||||
success(result) {
|
||||
if (result.authSetting[
|
||||
`scope.userLocation`
|
||||
]) {
|
||||
resove(true)
|
||||
} else {
|
||||
reject()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
// H5环境下提示用户手动在浏览器设置中开启位置权限
|
||||
that.showToast({
|
||||
title: '请在浏览器设置中手动开启位置权限'
|
||||
});
|
||||
reject(new Error('Location permission denied'));
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
return this.getAmapLocationMp();
|
||||
// #endif
|
||||
},
|
||||
// H5环境下使用腾讯地图API获取位置
|
||||
@@ -740,7 +606,22 @@ export default {
|
||||
// 获取到经纬度后,使用腾讯地图API进行逆地理编码
|
||||
const { latitude, longitude } = position.coords;
|
||||
|
||||
// 使用腾讯地图API获取详细地址信息
|
||||
let quotaExpire = uni.getStorageSync('tencent_geocoder_quota_expire');
|
||||
let quotaExceeded = quotaExpire && (now < quotaExpire);
|
||||
if (quotaExceeded) {
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
address: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: ''
|
||||
};
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
return;
|
||||
}
|
||||
const response = await that.fetchAddressFromTencent(latitude, longitude);
|
||||
|
||||
let locationInfo = {
|
||||
@@ -759,10 +640,18 @@ export default {
|
||||
resolve(locationInfo);
|
||||
} catch (error) {
|
||||
console.error('获取地址信息失败:', error);
|
||||
that.showToast({
|
||||
title: '获取地址信息失败'
|
||||
});
|
||||
reject(error);
|
||||
// 逆地理编码失败,但仍返回坐标信息
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
address: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: ''
|
||||
};
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
@@ -780,11 +669,252 @@ export default {
|
||||
);
|
||||
});
|
||||
},
|
||||
getTencentLocationMp: function() {
|
||||
let that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
let cachedLocation = uni.getStorageSync('cached_location');
|
||||
let cacheTime = uni.getStorageSync('location_cache_time');
|
||||
let now = new Date().getTime();
|
||||
if (cachedLocation && cacheTime && (now - cacheTime) < 30 * 60 * 1000) {
|
||||
resolve(cachedLocation);
|
||||
return;
|
||||
}
|
||||
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
isHighAccuracy: true,
|
||||
highAccuracyExpireTime: 10000,
|
||||
success: async (res) => {
|
||||
try {
|
||||
const { latitude, longitude } = res;
|
||||
let quotaExpire = uni.getStorageSync('tencent_geocoder_quota_expire');
|
||||
let quotaExceeded = quotaExpire && (now < quotaExpire);
|
||||
if (quotaExceeded) {
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
address: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: ''
|
||||
};
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
return;
|
||||
}
|
||||
const response = await that.fetchAddressFromTencent(latitude, longitude);
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
address: response.address || '',
|
||||
province: response.province || '',
|
||||
city: response.city || '',
|
||||
district: response.district || ''
|
||||
};
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
} catch (e) {
|
||||
console.error('逆地理编码失败:', e);
|
||||
// 逆地理编码失败,但仍返回坐标信息
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
address: '',
|
||||
province: '',
|
||||
city: '',
|
||||
district: ''
|
||||
};
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
}
|
||||
},
|
||||
fail: (e) => {
|
||||
console.error('获取位置失败:', e);
|
||||
uni.showModal({
|
||||
title: '地理位置授权',
|
||||
content: '为了更好的为您服务,请开启位置信息权限',
|
||||
confirmText: '去授权',
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
uni.openSetting({
|
||||
success(setting) {
|
||||
if (setting.authSetting && setting.authSetting['scope.userLocation']) {
|
||||
resolve(true);
|
||||
} else {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
getAmapLocationH5: function() {
|
||||
let that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
let cachedLocation = uni.getStorageSync('cached_location');
|
||||
let cacheTime = uni.getStorageSync('location_cache_time');
|
||||
let now = new Date().getTime();
|
||||
if (cachedLocation && cacheTime && (now - cacheTime) < 30 * 60 * 1000) {
|
||||
resolve(cachedLocation);
|
||||
return;
|
||||
}
|
||||
if (!navigator.geolocation) {
|
||||
that.showToast({ title: '您的浏览器不支持地理位置功能' });
|
||||
reject(new Error('Geolocation not supported'));
|
||||
return;
|
||||
}
|
||||
navigator.geolocation.getCurrentPosition(
|
||||
async (position) => {
|
||||
try {
|
||||
const { latitude, longitude } = position.coords;
|
||||
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: '' };
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
return;
|
||||
}
|
||||
const response = await that.fetchAddressFromAmap(latitude, longitude);
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
address: response.address || '',
|
||||
province: response.province || '',
|
||||
city: response.city || '',
|
||||
district: response.district || ''
|
||||
};
|
||||
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: '' };
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
console.error('获取位置失败:', error);
|
||||
that.showToast({ title: '获取位置失败,请检查浏览器位置权限设置' });
|
||||
reject(error);
|
||||
},
|
||||
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 30 * 60 * 1000 }
|
||||
);
|
||||
});
|
||||
},
|
||||
getAmapLocationMp: function() {
|
||||
let that = this;
|
||||
return new Promise((resolve, reject) => {
|
||||
let cachedLocation = uni.getStorageSync('cached_location');
|
||||
let cacheTime = uni.getStorageSync('location_cache_time');
|
||||
let now = new Date().getTime();
|
||||
if (cachedLocation && cacheTime && (now - cacheTime) < 30 * 60 * 1000) {
|
||||
resolve(cachedLocation);
|
||||
return;
|
||||
}
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
isHighAccuracy: true,
|
||||
highAccuracyExpireTime: 10000,
|
||||
success: async (res) => {
|
||||
try {
|
||||
const { latitude, longitude } = res;
|
||||
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: '' };
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
return;
|
||||
}
|
||||
const response = await that.fetchAddressFromAmap(latitude, longitude);
|
||||
let locationInfo = {
|
||||
lat: latitude,
|
||||
lng: longitude,
|
||||
address: response.address || '',
|
||||
province: response.province || '',
|
||||
city: response.city || '',
|
||||
district: response.district || ''
|
||||
};
|
||||
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: '' };
|
||||
uni.setStorageSync('cached_location', locationInfo);
|
||||
uni.setStorageSync('location_cache_time', now);
|
||||
resolve(locationInfo);
|
||||
}
|
||||
},
|
||||
fail: (e) => {
|
||||
console.error('获取位置失败:', e);
|
||||
uni.showModal({
|
||||
title: '地理位置授权',
|
||||
content: '为了更好的为您服务,请开启位置信息权限',
|
||||
confirmText: '去授权',
|
||||
success(res) {
|
||||
if (res.confirm) {
|
||||
uni.openSetting({
|
||||
success(setting) {
|
||||
if (setting.authSetting && setting.authSetting['scope.userLocation']) {
|
||||
resolve(true);
|
||||
} else {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
fetchAddressFromAmap: function(lat, lng) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const key = '0806ce19c290f720e7699ab73e8b9088';
|
||||
uni.request({
|
||||
url: 'https://restapi.amap.com/v3/geocode/regeo',
|
||||
data: { key, location: `${lng},${lat}`, extensions: 'base', radius: 1000 },
|
||||
success: (res) => {
|
||||
if (res.data && res.data.status == '1' && res.data.regeocode) {
|
||||
const comp = res.data.regeocode.addressComponent || {};
|
||||
const addr = res.data.regeocode.formatted_address || '';
|
||||
resolve({ province: comp.province || '', city: comp.city || '', district: comp.district || '', address: addr });
|
||||
} else {
|
||||
let info = res.data && res.data.info ? res.data.info : '';
|
||||
let now = new Date().getTime();
|
||||
if (info === 'DAILY_QUERY_OVER_LIMIT') {
|
||||
uni.setStorageSync('amap_geocoder_quota_expire', now + 24 * 60 * 60 * 1000);
|
||||
resolve({ province: '', city: '', district: '', address: '' });
|
||||
return;
|
||||
}
|
||||
reject(new Error('AMap geocoder error'));
|
||||
}
|
||||
},
|
||||
fail: (error) => { reject(error); }
|
||||
});
|
||||
});
|
||||
},
|
||||
// 使用腾讯地图API获取详细地址信息
|
||||
fetchAddressFromTencent: function(lat, lng) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 从manifest.json中获取腾讯地图API key
|
||||
const key = 'TPGBZ-VDUK3-ILX35-REJQK-5NBI2-UBF75';
|
||||
const key = '4THBZ-TH2CW-EZ5RQ-3FICO-VPU65-AQB7E';
|
||||
|
||||
// 使用腾讯地图逆地理编码API
|
||||
uni.request({
|
||||
@@ -803,9 +933,16 @@ export default {
|
||||
district: address_component.district,
|
||||
address: formatted_address
|
||||
});
|
||||
} else {
|
||||
reject(new Error('腾讯地图API返回错误'));
|
||||
}
|
||||
} else {
|
||||
let msg = res.data && res.data.message ? res.data.message : '';
|
||||
let now = new Date().getTime();
|
||||
if (res.data.status === 121 || (typeof msg === 'string' && msg.indexOf('每日调用量已达到上限') !== -1)) {
|
||||
uni.setStorageSync('tencent_geocoder_quota_expire', now + 24 * 60 * 60 * 1000);
|
||||
resolve({ province: '', city: '', district: '', address: '' });
|
||||
return;
|
||||
}
|
||||
reject(new Error('腾讯地图API返回错误'));
|
||||
}
|
||||
},
|
||||
fail: (error) => {
|
||||
reject(error);
|
||||
|
||||
Reference in New Issue
Block a user