fix:修复bug
This commit is contained in:
+319
-13
@@ -4,16 +4,16 @@ const DEFAULT_LOCALE = 'zh-CN'
|
||||
const permissionTexts = {
|
||||
'zh-CN': {
|
||||
locationTitle: '位置信息授权',
|
||||
locationNeed: '需要获取您的位置信息以展示附近设备,请在“设置-权限管理”中开启定位权限。',
|
||||
locationDenied: '未授权定位,将无法展示附近设备。您可以稍后在“设置-权限管理”中重新开启定位权限。',
|
||||
locationNeed: '需要获取您的位置信息以展示附近设备,请在"设置-权限管理"中开启定位权限。',
|
||||
locationDenied: '未授权定位,将无法展示附近设备。您可以稍后在"设置-权限管理"中重新开启定位权限。',
|
||||
goToSettings: '去设置',
|
||||
later: '暂不',
|
||||
gotIt: '知道了'
|
||||
},
|
||||
'en-US': {
|
||||
locationTitle: 'Location Permission',
|
||||
locationNeed: 'We need your location to show nearby devices. Please enable location in “Settings > Permissions”.',
|
||||
locationDenied: 'Location access is disabled. You can re-enable it later in “Settings > Permissions”.',
|
||||
locationNeed: 'We need your location to show nearby devices. Please enable location in "Settings > Permissions".',
|
||||
locationDenied: 'Location access is disabled. You can re-enable it later in "Settings > Permissions".',
|
||||
goToSettings: 'Set',
|
||||
later: 'Skip',
|
||||
gotIt: 'OK'
|
||||
@@ -37,7 +37,14 @@ const getPermissionText = (key) => {
|
||||
}
|
||||
|
||||
// 腾讯地图Key
|
||||
const QQMAP_KEY = 'RO5BZ-ECZ63-7US3C-RT5QW-TIDZE-2FF35';
|
||||
const QQMAP_KEY =
|
||||
// #ifdef H5
|
||||
'DJQBZ-WB53Q-WPS5B-4S6J7-53RMS-X4FJ2'
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
'RO5BZ-ECZ63-7US3C-RT5QW-TIDZE-2FF35'
|
||||
// #endif
|
||||
;
|
||||
|
||||
// 内联腾讯地图SDK核心代码
|
||||
const QQMapWX = (function() {
|
||||
@@ -118,7 +125,7 @@ const QQMapWX = (function() {
|
||||
}
|
||||
};
|
||||
if (!param.location) {
|
||||
wx.getLocation({
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: locationsuccess,
|
||||
fail: locationfail,
|
||||
@@ -280,10 +287,16 @@ const QQMapWX = (function() {
|
||||
|
||||
const locationsuccess = function (result) {
|
||||
requestParam.location = result.latitude + ',' + result.longitude;
|
||||
wx.request(Utils.buildWxRequestConfig(options, {
|
||||
// #ifdef H5
|
||||
// H5环境使用JSONP方式
|
||||
that._requestJsonp(URL_GET_GEOCODER, requestParam, options, 'reverseGeocoder');
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
uni.request(Utils.buildWxRequestConfig(options, {
|
||||
url: URL_GET_GEOCODER,
|
||||
data: requestParam
|
||||
}, 'reverseGeocoder'));
|
||||
// #endif
|
||||
};
|
||||
Utils.locationProcess(options, locationsuccess);
|
||||
}
|
||||
@@ -323,10 +336,16 @@ const QQMapWX = (function() {
|
||||
|
||||
const locationsuccess = function (result) {
|
||||
requestParam.boundary = "nearby(" + result.latitude + "," + result.longitude + "," + distance + "," + auto_extend + ")";
|
||||
wx.request(Utils.buildWxRequestConfig(options, {
|
||||
// #ifdef H5
|
||||
// H5环境使用JSONP方式
|
||||
that._requestJsonp(URL_SEARCH, requestParam, options, 'search');
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
uni.request(Utils.buildWxRequestConfig(options, {
|
||||
url: URL_SEARCH,
|
||||
data: requestParam
|
||||
}, 'search'));
|
||||
// #endif
|
||||
};
|
||||
|
||||
Utils.locationProcess(options, locationsuccess);
|
||||
@@ -368,17 +387,27 @@ const QQMapWX = (function() {
|
||||
if (options.location) {
|
||||
const locationsuccess = function (result) {
|
||||
requestParam.location = result.latitude + ',' + result.longitude;
|
||||
wx.request(Utils.buildWxRequestConfig(options, {
|
||||
// #ifdef H5
|
||||
that._requestJsonp(URL_SUGGESTION, requestParam, options, "suggest");
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
uni.request(Utils.buildWxRequestConfig(options, {
|
||||
url: URL_SUGGESTION,
|
||||
data: requestParam
|
||||
}, "suggest"));
|
||||
// #endif
|
||||
};
|
||||
Utils.locationProcess(options, locationsuccess);
|
||||
} else {
|
||||
wx.request(Utils.buildWxRequestConfig(options, {
|
||||
// #ifdef H5
|
||||
that._requestJsonp(URL_SUGGESTION, requestParam, options, "suggest");
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
uni.request(Utils.buildWxRequestConfig(options, {
|
||||
url: URL_SUGGESTION,
|
||||
data: requestParam
|
||||
}, "suggest"));
|
||||
// #endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -408,14 +437,64 @@ const QQMapWX = (function() {
|
||||
|
||||
const locationsuccess = function (result) {
|
||||
requestParam.from = result.latitude + ',' + result.longitude;
|
||||
wx.request(Utils.buildWxRequestConfig(options, {
|
||||
// #ifdef H5
|
||||
that._requestJsonp(URL_DISTANCE, requestParam, options, 'calculateDistance');
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
uni.request(Utils.buildWxRequestConfig(options, {
|
||||
url: URL_DISTANCE,
|
||||
data: requestParam
|
||||
}, 'calculateDistance'));
|
||||
// #endif
|
||||
};
|
||||
|
||||
Utils.locationProcess(options, locationsuccess);
|
||||
}
|
||||
|
||||
// H5环境JSONP请求方法
|
||||
_requestJsonp(url, params, options, feature) {
|
||||
const callbackName = `qqmap_callback_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
// 构建URL参数
|
||||
const queryString = Object.keys(params)
|
||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
||||
.join('&');
|
||||
|
||||
const fullUrl = `${url}?${queryString}&output=jsonp&callback=${callbackName}`;
|
||||
|
||||
// 创建全局回调函数
|
||||
window[callbackName] = (data) => {
|
||||
// 清理回调函数和script标签
|
||||
delete window[callbackName];
|
||||
const script = document.getElementById(callbackName);
|
||||
if (script && script.parentNode) {
|
||||
script.parentNode.removeChild(script);
|
||||
}
|
||||
|
||||
if (data.status === 0) {
|
||||
Utils.handleData(options, data, feature);
|
||||
} else {
|
||||
options.fail(data);
|
||||
}
|
||||
options.complete(data);
|
||||
};
|
||||
|
||||
// 创建script标签进行JSONP请求
|
||||
const script = document.createElement('script');
|
||||
script.id = callbackName;
|
||||
script.src = fullUrl;
|
||||
script.onerror = () => {
|
||||
delete window[callbackName];
|
||||
const scriptEl = document.getElementById(callbackName);
|
||||
if (scriptEl && scriptEl.parentNode) {
|
||||
scriptEl.parentNode.removeChild(scriptEl);
|
||||
}
|
||||
const error = { status: 600, message: '请求失败' };
|
||||
options.fail(error);
|
||||
options.complete(error);
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
}
|
||||
}
|
||||
|
||||
return QQMapWX;
|
||||
@@ -550,8 +629,9 @@ function getUserLocation() {
|
||||
});
|
||||
// #endif
|
||||
|
||||
// 非微信小程序平台:使用 uni.getLocation 做一个尽量兼容的兜底
|
||||
// 非微信小程序平台:根据平台使用不同的定位方式
|
||||
// #ifndef MP-WEIXIN
|
||||
// 统一使用 uni.getLocation,框架会根据环境自动选择最佳定位方式
|
||||
uni.getLocation({
|
||||
type: 'gcj02',
|
||||
success: (res) => {
|
||||
@@ -565,6 +645,16 @@ function getUserLocation() {
|
||||
},
|
||||
fail: (error) => {
|
||||
console.error('获取位置失败:', error);
|
||||
// H5 环境下的特殊错误提示
|
||||
// #ifdef H5
|
||||
if (error.errMsg && error.errMsg.indexOf('permission denied') !== -1) {
|
||||
uni.showModal({
|
||||
title: getPermissionText('locationTitle'),
|
||||
content: getPermissionText('locationNeed'),
|
||||
showCancel: false
|
||||
});
|
||||
}
|
||||
// #endif
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
@@ -575,6 +665,57 @@ function getUserLocation() {
|
||||
// 逆地理编码 - 根据经纬度获取地址信息
|
||||
function getRegeo(longitude, latitude) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifdef H5
|
||||
// H5环境:使用JSONP方式调用腾讯地图API,避免跨域问题
|
||||
const callbackName = `qqmap_geocoder_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
// 创建全局回调函数
|
||||
window[callbackName] = (data) => {
|
||||
// 清理回调函数和script标签
|
||||
delete window[callbackName];
|
||||
const script = document.getElementById(callbackName);
|
||||
if (script && script.parentNode) {
|
||||
script.parentNode.removeChild(script);
|
||||
}
|
||||
|
||||
if (data.status === 0) {
|
||||
const result = data.result;
|
||||
resolve({
|
||||
success: true,
|
||||
data: {
|
||||
formatted_address: result.address,
|
||||
addressComponent: {
|
||||
city: result.address_component.city,
|
||||
district: result.address_component.district,
|
||||
province: result.address_component.province,
|
||||
street: result.address_component.street,
|
||||
street_number: result.address_component.street_number
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.error('H5逆地理编码失败:', data);
|
||||
reject({ success: false, message: data.message || '逆地理编码失败' });
|
||||
}
|
||||
};
|
||||
|
||||
// 创建script标签进行JSONP请求
|
||||
const script = document.createElement('script');
|
||||
script.id = callbackName;
|
||||
script.src = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${QQMAP_KEY}&output=jsonp&callback=${callbackName}`;
|
||||
script.onerror = () => {
|
||||
delete window[callbackName];
|
||||
const scriptEl = document.getElementById(callbackName);
|
||||
if (scriptEl && scriptEl.parentNode) {
|
||||
scriptEl.parentNode.removeChild(scriptEl);
|
||||
}
|
||||
reject({ success: false, message: '逆地理编码请求失败' });
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// 小程序环境:使用 QQMapWX SDK
|
||||
const qqmap = getQQMapInstance();
|
||||
if (!qqmap) {
|
||||
reject({ success: false, message: '腾讯地图SDK未初始化' });
|
||||
@@ -608,12 +749,68 @@ function getRegeo(longitude, latitude) {
|
||||
reject({ success: false, message: error.message || '逆地理编码失败' });
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
});
|
||||
}
|
||||
|
||||
// 搜索周边POI
|
||||
function getPoiAround(longitude, latitude, keyword = '', radius = 1000) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifdef H5
|
||||
// H5环境:使用JSONP方式调用腾讯地图API
|
||||
const callbackName = `qqmap_search_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
// 创建全局回调函数
|
||||
window[callbackName] = (data) => {
|
||||
// 清理回调函数和script标签
|
||||
delete window[callbackName];
|
||||
const script = document.getElementById(callbackName);
|
||||
if (script && script.parentNode) {
|
||||
script.parentNode.removeChild(script);
|
||||
}
|
||||
|
||||
if (data.status === 0) {
|
||||
const searchSimplify = data.data.map(item => ({
|
||||
id: item.id || null,
|
||||
title: item.title || null,
|
||||
latitude: item.location && item.location.lat || null,
|
||||
longitude: item.location && item.location.lng || null,
|
||||
address: item.address || null,
|
||||
category: item.category || null,
|
||||
tel: item.tel || null,
|
||||
adcode: item.ad_info && item.ad_info.adcode || null,
|
||||
city: item.ad_info && item.ad_info.city || null,
|
||||
district: item.ad_info && item.ad_info.district || null,
|
||||
province: item.ad_info && item.ad_info.province || null
|
||||
}));
|
||||
|
||||
resolve({
|
||||
success: true,
|
||||
data: searchSimplify
|
||||
});
|
||||
} else {
|
||||
console.error('H5搜索POI失败:', data);
|
||||
reject({ success: false, message: data.message || '搜索POI失败' });
|
||||
}
|
||||
};
|
||||
|
||||
// 创建script标签进行JSONP请求
|
||||
const script = document.createElement('script');
|
||||
script.id = callbackName;
|
||||
script.src = `https://apis.map.qq.com/ws/place/v1/search?boundary=nearby(${latitude},${longitude},${radius})&keyword=${encodeURIComponent(keyword)}&orderby=_distance&page_size=10&page_index=1&key=${QQMAP_KEY}&output=jsonp&callback=${callbackName}`;
|
||||
script.onerror = () => {
|
||||
delete window[callbackName];
|
||||
const scriptEl = document.getElementById(callbackName);
|
||||
if (scriptEl && scriptEl.parentNode) {
|
||||
scriptEl.parentNode.removeChild(scriptEl);
|
||||
}
|
||||
reject({ success: false, message: '搜索POI请求失败' });
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// 小程序环境:使用 QQMapWX SDK
|
||||
const qqmap = getQQMapInstance();
|
||||
if (!qqmap) {
|
||||
reject({ success: false, message: '腾讯地图SDK未初始化' });
|
||||
@@ -639,12 +836,64 @@ function getPoiAround(longitude, latitude, keyword = '', radius = 1000) {
|
||||
reject({ success: false, message: error.message || '搜索POI失败' });
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
});
|
||||
}
|
||||
|
||||
// 计算距离(异步)
|
||||
function calculateDistance(from, to) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifdef H5
|
||||
// H5环境:使用JSONP方式调用腾讯地图API
|
||||
const callbackName = `qqmap_distance_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
// 创建全局回调函数
|
||||
window[callbackName] = (data) => {
|
||||
// 清理回调函数和script标签
|
||||
delete window[callbackName];
|
||||
const script = document.getElementById(callbackName);
|
||||
if (script && script.parentNode) {
|
||||
script.parentNode.removeChild(script);
|
||||
}
|
||||
|
||||
if (data.status === 0) {
|
||||
const distances = data.result.elements.map(element => element.distance);
|
||||
resolve({
|
||||
success: true,
|
||||
data: distances
|
||||
});
|
||||
} else {
|
||||
console.error('H5计算距离失败:', data);
|
||||
reject({ success: false, message: data.message || '计算距离失败' });
|
||||
}
|
||||
};
|
||||
|
||||
// 创建script标签进行JSONP请求
|
||||
const script = document.createElement('script');
|
||||
script.id = callbackName;
|
||||
// 构建to参数
|
||||
let toStr = '';
|
||||
if (Array.isArray(to)) {
|
||||
toStr = to.map(p => `${p.latitude},${p.longitude}`).join(';');
|
||||
} else {
|
||||
toStr = `${to.latitude},${to.longitude}`;
|
||||
}
|
||||
|
||||
const fromStr = `${from.latitude},${from.longitude}`;
|
||||
script.src = `https://apis.map.qq.com/ws/distance/v1/?mode=walking&from=${fromStr}&to=${toStr}&key=${QQMAP_KEY}&output=jsonp&callback=${callbackName}`;
|
||||
script.onerror = () => {
|
||||
delete window[callbackName];
|
||||
const scriptEl = document.getElementById(callbackName);
|
||||
if (scriptEl && scriptEl.parentNode) {
|
||||
scriptEl.parentNode.removeChild(scriptEl);
|
||||
}
|
||||
reject({ success: false, message: '计算距离请求失败' });
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// 小程序环境:使用 QQMapWX SDK
|
||||
const qqmap = getQQMapInstance();
|
||||
if (!qqmap) {
|
||||
reject({ success: false, message: '腾讯地图SDK未初始化' });
|
||||
@@ -667,6 +916,7 @@ function calculateDistance(from, to) {
|
||||
reject({ success: false, message: error.message || '计算距离失败' });
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
});
|
||||
}
|
||||
|
||||
@@ -685,6 +935,61 @@ function calculateDistanceSync(lat1, lng1, lat2, lng2) {
|
||||
// 关键词提示
|
||||
function getSuggestion(keyword, region = '全国') {
|
||||
return new Promise((resolve, reject) => {
|
||||
// #ifdef H5
|
||||
// H5环境:使用JSONP方式调用腾讯地图API
|
||||
const callbackName = `qqmap_suggestion_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
|
||||
// 创建全局回调函数
|
||||
window[callbackName] = (data) => {
|
||||
// 清理回调函数和script标签
|
||||
delete window[callbackName];
|
||||
const script = document.getElementById(callbackName);
|
||||
if (script && script.parentNode) {
|
||||
script.parentNode.removeChild(script);
|
||||
}
|
||||
|
||||
if (data.status === 0) {
|
||||
const suggestSimplify = data.data.map(item => ({
|
||||
adcode: item.adcode || null,
|
||||
address: item.address || null,
|
||||
category: item.category || null,
|
||||
city: item.city || null,
|
||||
district: item.district || null,
|
||||
id: item.id || null,
|
||||
latitude: item.location && item.location.lat || null,
|
||||
longitude: item.location && item.location.lng || null,
|
||||
province: item.province || null,
|
||||
title: item.title || null,
|
||||
type: item.type || null
|
||||
}));
|
||||
|
||||
resolve({
|
||||
success: true,
|
||||
data: suggestSimplify
|
||||
});
|
||||
} else {
|
||||
console.error('H5关键词提示失败:', data);
|
||||
reject({ success: false, message: data.message || '关键词提示失败' });
|
||||
}
|
||||
};
|
||||
|
||||
// 创建script标签进行JSONP请求
|
||||
const script = document.createElement('script');
|
||||
script.id = callbackName;
|
||||
script.src = `https://apis.map.qq.com/ws/place/v1/suggestion?keyword=${encodeURIComponent(keyword)}®ion=${encodeURIComponent(region)}&page_size=10&page_index=1&key=${QQMAP_KEY}&output=jsonp&callback=${callbackName}`;
|
||||
script.onerror = () => {
|
||||
delete window[callbackName];
|
||||
const scriptEl = document.getElementById(callbackName);
|
||||
if (scriptEl && scriptEl.parentNode) {
|
||||
scriptEl.parentNode.removeChild(scriptEl);
|
||||
}
|
||||
reject({ success: false, message: '关键词提示请求失败' });
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
// #endif
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
// 小程序环境:使用 QQMapWX SDK
|
||||
const qqmap = getQQMapInstance();
|
||||
if (!qqmap) {
|
||||
reject({ success: false, message: '腾讯地图SDK未初始化' });
|
||||
@@ -706,6 +1011,7 @@ function getSuggestion(keyword, region = '全国') {
|
||||
reject({ success: false, message: error.message || '关键词提示失败' });
|
||||
}
|
||||
});
|
||||
// #endif
|
||||
});
|
||||
}
|
||||
|
||||
@@ -732,4 +1038,4 @@ export function testDistanceCalculation() {
|
||||
console.log('转换为公里:', (distance / 1000).toFixed(2), '公里');
|
||||
|
||||
return distance;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user