修改定位

main
kangwenjing 2026-03-26 17:33:55 +08:00
parent 8606a02971
commit 0ca5070755
2 changed files with 22 additions and 2 deletions

View File

@ -87,7 +87,7 @@ export async function resolveTencentMapLocation() {
return bestResult; return bestResult;
} }
throw browserError || sdkError || new Error("定位失败,请稍后重试。"); throw new Error(buildLocationFailureMessage(browserError, sdkError));
} }
async function resolveWithTencentSdk() { async function resolveWithTencentSdk() {
@ -379,6 +379,26 @@ function getBrowserLocationErrorMessage(error?: GeolocationPositionError | null)
} }
} }
function buildLocationFailureMessage(browserError: Error | null, sdkError: Error | null) {
const reasons: string[] = [];
if (!window.isSecureContext) {
reasons.push("当前页面未被浏览器视为安全环境,请使用已信任的 HTTPS 地址打开。");
}
if (browserError) {
reasons.push(`浏览器定位:${browserError.message}`);
}
if (sdkError) {
reasons.push(`腾讯定位:${sdkError.message}`);
}
if (!reasons.length) {
return "定位失败,请稍后重试。";
}
return reasons.join("");
}
export function wgs84ToGcj02(latitude: number, longitude: number) { export function wgs84ToGcj02(latitude: number, longitude: number) {
if (isOutsideChina(latitude, longitude)) { if (isOutsideChina(latitude, longitude)) {
return { latitude, longitude }; return { latitude, longitude };

View File

@ -354,7 +354,7 @@ export default function Work() {
setCheckInError(""); setCheckInError("");
setRefreshingLocation(true); setRefreshingLocation(true);
if (!window.isSecureContext && location.hostname !== "localhost" && location.hostname !== "127.0.0.1") { if (!window.isSecureContext && location.hostname !== "localhost" && location.hostname !== "127.0.0.1") {
setLocationHint("当前地址不是 HTTPS手机浏览器会拦截定位。请使用 HTTPS 地址打开。"); setLocationHint("当前页面未被浏览器视为安全环境,常见原因是 HTTP 或证书未被手机信任。请使用已信任的 HTTPS 地址打开。");
setRefreshingLocation(false); setRefreshingLocation(false);
return; return;
} }