diff --git a/packages/uni-h5/src/helpers/location.ts b/packages/uni-h5/src/helpers/location.ts index 7eb44d12ab136ec72276ddb3ad79cc10342ac3ea..dbd43bbf78b46b0ffbb9f30365af45220d9ca745 100644 --- a/packages/uni-h5/src/helpers/location.ts +++ b/packages/uni-h5/src/helpers/location.ts @@ -1,3 +1,7 @@ +import { extend } from '@vue/shared' +import { getJSONP } from './getJSONP' +import { loadMaps } from '../view/components/map/maps' + export interface Point { latitude: number longitude: number @@ -18,6 +22,8 @@ export enum MapType { UNKNOWN = '', } +export type GeoRes = (coords: GeolocationCoordinates, skip?: boolean) => void + export function getMapInfo() { if (__uniConfig.qqMapKey) { return { @@ -55,3 +61,71 @@ export const getIsAMap = () => { return (IS_AMAP = getMapInfo().type === MapType.AMAP) } } + +export function translateGeo( + type: string | undefined, + coords: GeolocationCoordinates, + skip?: boolean +) { + const mapInfo = getMapInfo() + const wgs84Map = [MapType.GOOGLE] + if ( + (type && type.toUpperCase() === 'WGS84') || + wgs84Map.includes(mapInfo.type) || + skip + ) { + return Promise.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) + } + } + ) + }) + }) + } + return Promise.reject(new Error('translateGeo faild')) +} diff --git a/packages/uni-h5/src/service/api/location/getLocation.ts b/packages/uni-h5/src/service/api/location/getLocation.ts index ab16f441eac202e90c729e6889d2503b27c353d9..c40b1cb872947434dc8c1074da212f1ae2e017d6 100644 --- a/packages/uni-h5/src/service/api/location/getLocation.ts +++ b/packages/uni-h5/src/service/api/location/getLocation.ts @@ -1,4 +1,3 @@ -import { extend } from '@vue/shared' import { defineAsyncApi, API_GET_LOCATION, @@ -6,12 +5,14 @@ import { GetLocationProtocol, GetLocationOptions, } from '@dcloudio/uni-api' -import { MapType, getMapInfo } from '../../../helpers/location' +import { + MapType, + getMapInfo, + GeoRes, + translateGeo, +} 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 export const getLocation = defineAsyncApi( API_GET_LOCATION, @@ -89,80 +90,22 @@ export const getLocation = defineAsyncApi( }) }) .then((coords: GeolocationCoordinates, skip?: boolean) => { - const wgs84Map = [MapType.GOOGLE] - if ( - (type && type.toUpperCase() === 'WGS84') || - wgs84Map.includes(mapInfo.type) || - skip - ) { - return 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) - } - } - ) + translateGeo(type, coords, skip) + .then((coords: GeolocationCoordinates | any) => { + resolve({ + latitude: coords.latitude, + longitude: coords.longitude, + accuracy: coords.accuracy, + speed: coords.altitude || 0, + altitude: coords.altitude || 0, + verticalAccuracy: coords.altitudeAccuracy || 0, + // 无专门水平精度,使用位置精度替代 + horizontalAccuracy: coords.accuracy || 0, }) }) - } - }) - .then((coords: GeolocationCoordinates | any) => { - resolve({ - latitude: coords.latitude, - longitude: coords.longitude, - accuracy: coords.accuracy, - speed: coords.altitude || 0, - altitude: coords.altitude || 0, - verticalAccuracy: coords.altitudeAccuracy || 0, - // 无专门水平精度,使用位置精度替代 - horizontalAccuracy: coords.accuracy || 0, - }) - }) - .catch((error) => { - reject(error.message) + .catch((error) => { + reject(error.message) + }) }) }, GetLocationProtocol,