diff --git a/packages/uni-h5/src/service/api/location/getLocation.ts b/packages/uni-h5/src/service/api/location/getLocation.ts index 70751bdd0097bc322bdd2922a1bfbf78c7aaa86d..894deb9d7fe360a1d8e8972c31aaaa22cd188e1c 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,