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

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

上级 7c33a89d
......@@ -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 = <API_TYPE_GET_LOCATION>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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册