提交 1b928357 编写于 作者: DCloud-WZF's avatar DCloud-WZF 💬

feat: H5高德地图增加坐标系转换并移除ip定位逻辑

上级 7c33a89d
...@@ -9,6 +9,7 @@ import { ...@@ -9,6 +9,7 @@ import {
import { MapType, getMapInfo } from '../../../helpers/location' import { MapType, getMapInfo } 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 type GeoRes = (coords: GeolocationCoordinates, skip?: boolean) => void
...@@ -82,65 +83,73 @@ export const getLocation = <API_TYPE_GET_LOCATION>defineAsyncApi( ...@@ -82,65 +83,73 @@ export const getLocation = <API_TYPE_GET_LOCATION>defineAsyncApi(
reject(new Error('network error')) 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 { } else {
reject(error) reject(error)
} }
}) })
}) })
.then((coords: GeolocationCoordinates, skip?: boolean) => { .then((coords: GeolocationCoordinates, skip?: boolean) => {
const wgs84Map = [MapType.GOOGLE]
if ( if (
(type && type.toUpperCase() === 'WGS84') || (type && type.toUpperCase() === 'WGS84') ||
mapInfo.type !== MapType.QQ || wgs84Map.includes(mapInfo.type) ||
skip skip
) { ) {
return coords return coords
} }
return new Promise((resolve: GeoRes) => { if (mapInfo.type === MapType.QQ) {
getJSONP( return new Promise((resolve: GeoRes) => {
`https://apis.map.qq.com/jsapi?qt=translate&type=1&points=${coords.longitude},${coords.latitude}&key=${mapInfo.key}&output=jsonp&pf=jsapi&ref=jsapi`, 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', {
}, callback: 'cb',
(res: any) => { },
if ( (res: any) => {
'detail' in res && if (
'points' in res.detail && 'detail' in res &&
res.detail.points.length 'points' in res.detail &&
) { res.detail.points.length
const location = res.detail.points[0] ) {
resolve( const location = res.detail.points[0]
extend({}, coords, { resolve(
longitude: location.lng, extend({}, coords, {
latitude: location.lat, longitude: location.lng,
}) latitude: location.lat,
) })
} else { )
resolve(coords) } else {
} resolve(coords)
}, }
() => 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({ resolve({
latitude: coords.latitude, latitude: coords.latitude,
longitude: coords.longitude, longitude: coords.longitude,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册