提交 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 {
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'))
}
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 = <API_TYPE_GET_LOCATION>defineAsyncApi(
API_GET_LOCATION,
......@@ -89,80 +90,22 @@ export const getLocation = <API_TYPE_GET_LOCATION>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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册