From bba3c53ad3e32611cedfd864866cedcdd02ba16b Mon Sep 17 00:00:00 2001 From: codexu <461229187@qq.com> Date: Thu, 8 Sep 2022 18:13:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E5=AE=9A=E4=BD=8D?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=9D=90=E6=A0=87=E8=BD=AC=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/platforms/h5/helpers/location.js | 74 +++++++++++++++++++ .../h5/service/api/location/get-location.js | 72 ++++-------------- .../service/api/location/location-change.js | 23 ++++-- 3 files changed, 107 insertions(+), 62 deletions(-) diff --git a/src/platforms/h5/helpers/location.js b/src/platforms/h5/helpers/location.js index f00476e936..e7fecbb125 100644 --- a/src/platforms/h5/helpers/location.js +++ b/src/platforms/h5/helpers/location.js @@ -1,3 +1,6 @@ +import { getJSONP } from './get-jsonp' +import { loadMaps } from '../view/components/map/maps' + export const ICON_PATH_ORIGIN = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIQAAACECAMAAABmmnOVAAAC01BMVEUAAAAAef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef8Aef96quGStdqStdpbnujMzMzCyM7Gyc7Ky83MzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzM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Yj8/APG67Rie8pUDsAAAAASUVORK5CYII=' export const ICON_PATH_TARGET = @@ -38,3 +41,74 @@ export function getMapInfo () { } export const IS_AMAP = getMapInfo().type === MapType.AMAP + +export function translateGeo (type, coords, skip) { + const mapInfo = getMapInfo() + const wgs84Map = [MapType.GOOGLE] + if ( + type.toUpperCase() === 'WGS84' || + wgs84Map.includes(mapInfo.type) || + skip + ) { + return Promise.resolve(coords) + } + + if (mapInfo.type === MapType.QQ) { + 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({ + longitude: location.lng, + latitude: location.lat, + altitude: coords.altitude, + accuracy: coords.accuracy, + altitudeAccuracy: coords.altitudeAccuracy, + heading: coords.heading, + speed: coords.speed + }) + } else { + reject(new Error('translate geo fail')) + } + }, + () => reject(new Error('translate geo fail')) + ) + }) + } + + if (mapInfo.type === MapType.AMAP) { + return new Promise((resolve, reject) => { + loadMaps([], () => { + window.AMap.convertFrom( + [coords.longitude, coords.latitude], + 'gps', + (_, res) => { + if (res.info === 'ok' && res.locations.length) { + const { lat, lng } = res.locations[0] + resolve({ + longitude: lng, + latitude: lat, + altitude: coords.altitude, + accuracy: coords.accuracy, + altitudeAccuracy: coords.altitudeAccuracy, + heading: coords.heading, + speed: coords.speed + }) + } else { + reject(new Error('translate geo fail')) + } + } + ) + }) + }) + } +} diff --git a/src/platforms/h5/service/api/location/get-location.js b/src/platforms/h5/service/api/location/get-location.js index 64f40c434e..20eaa2db37 100644 --- a/src/platforms/h5/service/api/location/get-location.js +++ b/src/platforms/h5/service/api/location/get-location.js @@ -3,9 +3,9 @@ import { } from '../../../helpers/get-jsonp' import { MapType, - getMapInfo + getMapInfo, + translateGeo } from '../../../helpers/location' -import { loadMaps } from '../../../view/components/map/maps' /** * 获取定位信息 @@ -73,62 +73,22 @@ export function getLocation ({ } }) }).then((coords, skip) => { - const wgs84Map = [MapType.GOOGLE] - if (type.toUpperCase() === 'WGS84' || wgs84Map.includes(mapInfo.type) || skip) { - return 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)) + translateGeo(type, coords, skip) + .then(coords => { + invoke( + callbackId, + Object.assign(coords, { + errMsg: 'getLocation:ok', + verticalAccuracy: coords.altitudeAccuracy || 0, + // 无专门水平精度,使用位置精度替代 + horizontalAccuracy: coords.accuracy + }) + ) }) - } - - 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) - } - } - ) + .catch(error => { + invoke(callbackId, { + errMsg: 'getLocation:fail ' + error.message }) }) - } - }).then(coords => { - invoke(callbackId, Object.assign(coords, { - errMsg: 'getLocation:ok', - verticalAccuracy: coords.altitudeAccuracy || 0, - // 无专门水平精度,使用位置精度替代 - horizontalAccuracy: coords.accuracy - })) - }).catch(error => { - invoke(callbackId, { - errMsg: 'getLocation:fail ' + error.message - }) }) } diff --git a/src/platforms/h5/service/api/location/location-change.js b/src/platforms/h5/service/api/location/location-change.js index 75a6d5d7ca..8f41243408 100644 --- a/src/platforms/h5/service/api/location/location-change.js +++ b/src/platforms/h5/service/api/location/location-change.js @@ -1,3 +1,5 @@ +import { translateGeo } from '../../../helpers/location' + const { invokeCallbackHandler: invoke } = UniServiceJSBridge const callbackIds = [] const callbackOnErrorIds = [] @@ -7,18 +9,27 @@ let watchId /** * 开始更新定位 */ -export function startLocationUpdate () { +export function startLocationUpdate ({ type = 'wgs84' }) { if (navigator.geolocation) { watchId = navigator.geolocation.watchPosition( res => { - callbackIds.forEach(callbackId => { - invoke(callbackId, res.coords) - }) + translateGeo(type, res.coords) + .then((coords) => { + callbackIds.forEach(callbackId => { + invoke(callbackId, coords) + }) + }).catch(error => { + callbackOnErrorIds.forEach(callbackId => { + invoke(callbackId, { + errMsg: 'onLocationChange:fail' + error.message + }) + }) + }) }, error => { callbackOnErrorIds.forEach(callbackId => { invoke(callbackId, { - errMsg: 'getLocation:fail' + error.message + errMsg: 'onLocationChange:fail' + error.message }) }) } @@ -26,7 +37,7 @@ export function startLocationUpdate () { } else { callbackOnErrorIds.forEach(callbackId => { invoke(callbackId, { - errMsg: 'getLocation:fail device nonsupport geolocation' + errMsg: 'onLocationChange:fail device nonsupport geolocation' }) }) } -- GitLab