提交 4635897f 编写于 作者: C codexu

refactor(h5): 拆分函数 translateGeo,通过 type 转换坐标

上级 94ab5fa0
import { extend } from '@vue/shared'
import { getJSONP } from './getJSONP'
import { loadMaps } from '../view/components/map/maps'
export interface Point { export interface Point {
latitude: number latitude: number
longitude: number longitude: number
...@@ -18,6 +22,8 @@ export enum MapType { ...@@ -18,6 +22,8 @@ export enum MapType {
UNKNOWN = '', UNKNOWN = '',
} }
export type GeoRes = (coords: GeolocationCoordinates, skip?: boolean) => void
export function getMapInfo() { export function getMapInfo() {
if (__uniConfig.qqMapKey) { if (__uniConfig.qqMapKey) {
return { return {
...@@ -55,3 +61,71 @@ export const getIsAMap = () => { ...@@ -55,3 +61,71 @@ export const getIsAMap = () => {
return (IS_AMAP = getMapInfo().type === MapType.AMAP) 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'))
}
import { extend } from '@vue/shared'
import { import {
defineAsyncApi, defineAsyncApi,
API_GET_LOCATION, API_GET_LOCATION,
...@@ -6,12 +5,14 @@ import { ...@@ -6,12 +5,14 @@ import {
GetLocationProtocol, GetLocationProtocol,
GetLocationOptions, GetLocationOptions,
} from '@dcloudio/uni-api' } from '@dcloudio/uni-api'
import { MapType, getMapInfo } from '../../../helpers/location' import {
MapType,
getMapInfo,
GeoRes,
translateGeo,
} from '../../../helpers/location'
import { getJSONP } from '../../../helpers/getJSONP' import { getJSONP } from '../../../helpers/getJSONP'
import { request } from '../network/request' import { request } from '../network/request'
import { loadMaps } from '../../../view/components/map/maps'
type GeoRes = (coords: GeolocationCoordinates, skip?: boolean) => void
export const getLocation = <API_TYPE_GET_LOCATION>defineAsyncApi( export const getLocation = <API_TYPE_GET_LOCATION>defineAsyncApi(
API_GET_LOCATION, API_GET_LOCATION,
...@@ -89,80 +90,22 @@ export const getLocation = <API_TYPE_GET_LOCATION>defineAsyncApi( ...@@ -89,80 +90,22 @@ export const getLocation = <API_TYPE_GET_LOCATION>defineAsyncApi(
}) })
}) })
.then((coords: GeolocationCoordinates, skip?: boolean) => { .then((coords: GeolocationCoordinates, skip?: boolean) => {
const wgs84Map = [MapType.GOOGLE] translateGeo(type, coords, skip)
if ( .then((coords: GeolocationCoordinates | any) => {
(type && type.toUpperCase() === 'WGS84') || resolve({
wgs84Map.includes(mapInfo.type) || latitude: coords.latitude,
skip longitude: coords.longitude,
) { accuracy: coords.accuracy,
return coords speed: coords.altitude || 0,
} altitude: coords.altitude || 0,
if (mapInfo.type === MapType.QQ) { verticalAccuracy: coords.altitudeAccuracy || 0,
return new Promise((resolve: GeoRes) => { // 无专门水平精度,使用位置精度替代
getJSONP( horizontalAccuracy: coords.accuracy || 0,
`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)
}
}
)
}) })
}) })
} .catch((error) => {
}) reject(error.message)
.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)
}) })
}, },
GetLocationProtocol, GetLocationProtocol,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册