From b87ad91d983962ae8099f7067334703b692683b8 Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Fri, 26 Aug 2022 14:50:03 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20H5=E9=AB=98=E5=BE=B7=E5=9C=B0=E5=9B=BE?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9D=90=E6=A0=87=E7=B3=BB=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=E5=B9=B6=E7=A7=BB=E9=99=A4ip=E5=AE=9A=E4=BD=8D=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../h5/service/api/location/get-location.js | 76 +++++++++++-------- 1 file changed, 45 insertions(+), 31 deletions(-) diff --git a/src/platforms/h5/service/api/location/get-location.js b/src/platforms/h5/service/api/location/get-location.js index 1208390ab..64f40c434 100644 --- a/src/platforms/h5/service/api/location/get-location.js +++ b/src/platforms/h5/service/api/location/get-location.js @@ -5,6 +5,7 @@ import { MapType, getMapInfo } from '../../../helpers/location' +import { loadMaps } from '../../../view/components/map/maps' /** * 获取定位信息 @@ -67,44 +68,57 @@ export function getLocation ({ reject(new Error('network error')) } }) - } else if (mapInfo.type === MapType.AMAP) { - window.AMap.plugin('AMap.Geolocation', function () { - var geolocation = new window.AMap.Geolocation({}) - geolocation.getCurrentPosition(function (status, res) { - if (status === 'complete') { - resolve({ - latitude: res.position.lat, - longitude: res.position.lng, - accuracy: res.accuracy - }) - } else { - reject(new Error((res.message) || JSON.stringify(res))) - } - }) - }) } else { reject(new Error('network error')) } }) }).then((coords, skip) => { - if (type.toUpperCase() === 'WGS84' || mapInfo.type !== MapType.QQ || skip) { + const wgs84Map = [MapType.GOOGLE] + if (type.toUpperCase() === 'WGS84' || wgs84Map.includes(mapInfo.type) || skip) { return coords } - return new Promise((resolve, reject) => { - 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) => { - if ('detail' in res && 'points' in res.detail && res.detail.points.length) { - const location = res.detail.points[0] - resolve(Object.assign({}, coords, { - longitude: location.lng, - latitude: location.lat - })) - } else { - resolve(coords) - } - }, () => resolve(coords)) - }) + + if (mapInfo.type === MapType.QQ) { + return new Promise((resolve) => { + 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) => { + if ('detail' in res && 'points' in res.detail && res.detail.points.length) { + const location = res.detail.points[0] + resolve(Object.assign({}, coords, { + longitude: location.lng, + latitude: location.lat + })) + } else { + resolve(coords) + } + }, () => resolve(coords)) + }) + } + + if (mapInfo.type === MapType.AMAP) { + return new Promise((resolve) => { + loadMaps([], () => { + window.AMap.convertFrom( + [coords.longitude, coords.latitude], + 'gps', + (_, res) => { + if (res.info === 'ok' && res.locations.length) { + const { lat, lng } = res.locations[0] + resolve( + Object.assign({}, coords, { + longitude: lng, + latitude: lat + }) + ) + } else { + resolve(coords) + } + } + ) + }) + }) + } }).then(coords => { invoke(callbackId, Object.assign(coords, { errMsg: 'getLocation:ok', -- GitLab