From 1b92835743f3f5a7c256dcbfbeefd05af7f025a8 Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Fri, 26 Aug 2022 14:50:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20H5=E9=AB=98=E5=BE=B7=E5=9C=B0=E5=9B=BE?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9D=90=E6=A0=87=E7=B3=BB=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=B9=B6=E7=A7=BB=E9=99=A4ip=E5=AE=9A=E4=BD=8D=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/service/api/location/getLocation.ts | 101 ++++++++++-------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/packages/uni-h5/src/service/api/location/getLocation.ts b/packages/uni-h5/src/service/api/location/getLocation.ts index 70751bdd0..894deb9d7 100644 --- a/packages/uni-h5/src/service/api/location/getLocation.ts +++ b/packages/uni-h5/src/service/api/location/getLocation.ts @@ -9,6 +9,7 @@ import { import { MapType, getMapInfo } from '../../../helpers/location' import { getJSONP } from '../../../helpers/getJSONP' import { request } from '../network/request' +import { loadMaps } from '../../../view/components/map/maps' type GeoRes = (coords: GeolocationCoordinates, skip?: boolean) => void @@ -82,65 +83,73 @@ export const getLocation = defineAsyncApi( reject(new Error('network error')) }, }) - } else if (mapInfo.type === MapType.AMAP) { - window.AMap.plugin('AMap.Geolocation', function () { - var geolocation = new (window.AMap as any).Geolocation({}) - geolocation.getCurrentPosition(function ( - status: string, - res: any - ) { - if (status === 'complete') { - resolve({ - latitude: res.position.lat, - longitude: res.position.lng, - accuracy: res.accuracy, - } as GeolocationCoordinates) - } else { - reject(new Error(res.message || JSON.stringify(res))) - } - }) - }) } else { reject(error) } }) }) .then((coords: GeolocationCoordinates, skip?: boolean) => { + const wgs84Map = [MapType.GOOGLE] if ( (type && type.toUpperCase() === 'WGS84') || - mapInfo.type !== MapType.QQ || + wgs84Map.includes(mapInfo.type) || skip ) { return coords } - return new Promise((resolve: GeoRes) => { - getJSONP( - `https://apis.map.qq.com/jsapi?qt=translate&type=1&points=${coords.longitude},${coords.latitude}&key=${mapInfo.key}&output=jsonp&pf=jsapi&ref=jsapi`, - { - callback: 'cb', - }, - (res: any) => { - if ( - 'detail' in res && - 'points' in res.detail && - res.detail.points.length - ) { - const location = res.detail.points[0] - resolve( - extend({}, coords, { - longitude: location.lng, - latitude: location.lat, - }) - ) - } else { - resolve(coords) - } - }, - () => resolve(coords) - ) - }) + if (mapInfo.type === MapType.QQ) { + return new Promise((resolve: GeoRes) => { + getJSONP( + `https://apis.map.qq.com/jsapi?qt=translate&type=1&points=${coords.longitude},${coords.latitude}&key=${mapInfo.key}&output=jsonp&pf=jsapi&ref=jsapi`, + { + callback: 'cb', + }, + (res: any) => { + if ( + 'detail' in res && + 'points' in res.detail && + res.detail.points.length + ) { + const location = res.detail.points[0] + resolve( + extend({}, coords, { + longitude: location.lng, + latitude: location.lat, + }) + ) + } else { + resolve(coords) + } + }, + () => resolve(coords) + ) + }) + } + if (mapInfo.type === MapType.AMAP) { + return new Promise((resolve: GeoRes) => { + loadMaps([], () => { + window.AMap.convertFrom( + [coords.longitude, coords.latitude], + 'gps', + (_: string, res: any) => { + if(res.info === 'ok' && res.locations.length){ + const {lat,lng} = res.locations[0] + resolve( + extend({}, coords, { + longitude: lng, + latitude: lat, + }) + ) + }else{ + resolve(coords) + } + } + ) + }) + }) + } }) - .then((coords: GeolocationCoordinates) => { + .then((coords: GeolocationCoordinates|any) => { resolve({ latitude: coords.latitude, longitude: coords.longitude, -- GitLab