diff --git a/src/platforms/h5/service/api/location/get-location.js b/src/platforms/h5/service/api/location/get-location.js index a9c6119053df670fa6bc537dd75fbae891bb9de8..56cbdc3b4b57e9a9019730c99d7b085c6be41896 100644 --- a/src/platforms/h5/service/api/location/get-location.js +++ b/src/platforms/h5/service/api/location/get-location.js @@ -1,33 +1,10 @@ import { getJSONP } from '../../../helpers/get-jsonp' -/** - * wgs84坐标转Gcj02坐标 - * @param {object} coords - * @param {Function} success - * @param {Function} error - */ -function wgs84ToGcj02 (coords, success, error) { - /** - * uniapp 内置key - */ - var key = __uniConfig.qqMapKey - var url = - `https://apis.map.qq.com/ws/coord/v1/translate?locations=${coords.latitude},${coords.longitude}&type=1&key=${key}&output=jsonp` - getJSONP(url, {}, (res) => { - if ('locations' in res && res.locations.length) { - success({ - longitude: res.locations[0].lng, - latitude: res.locations[0].lat - }) - } else { - error(res) - } - }, error) -} + /** * 获取定位信息 - * @param {*} param0 + * @param {*} options * @param {*} callbackId */ export function getLocation ({ @@ -37,38 +14,60 @@ export function getLocation ({ const { invokeCallbackHandler: invoke } = UniServiceJSBridge + const key = __uniConfig.qqMapKey - function callback (coords) { + new Promise((resolve, reject) => { + if (navigator.geolocation) { + navigator.geolocation.getCurrentPosition(res => resolve(res.coords), reject, { + enableHighAccuracy: altitude, + timeout: 1000 * 60 * 5 + }) + } else { + reject(new Error('device nonsupport geolocation')) + } + }).catch(() => { + return new Promise((resolve, reject) => { + getJSONP(`https://apis.map.qq.com/ws/location/v1/ip?output=jsonp&key=${key}`, { + callback: 'callback' + }, (res) => { + if ('result' in res && res.result.location) { + const location = res.result.location + resolve({ + latitude: location.lat, + longitude: location.lng + }) + } else { + reject(new Error(JSON.stringify(res))) + } + }, () => reject(new Error('network error'))) + }) + }).then(coords => { + if (type.toUpperCase() === 'WGS84') { + return coords + } + return new Promise((resolve, reject) => { + getJSONP(`https://apis.map.qq.com/ws/coord/v1/translate?locations=${coords.latitude},${coords.longitude}&type=1&key=${key}&output=jsonp`, {}, (res) => { + if ('locations' in res && res.locations.length) { + const location = res.locations[0] + resolve(Object.assign({}, coords, { + longitude: location.lng, + latitude: location.lat + })) + } else { + reject(new Error(JSON.stringify(res))) + } + }, () => reject(new Error('network error'))) + }) + }).then(coords => { invoke(callbackId, Object.assign(coords, { errMsg: 'getLocation:ok', verticalAccuracy: coords.altitudeAccuracy || 0, // 无专门水平精度,使用位置精度替代 horizontalAccuracy: coords.accuracy })) - } - if (navigator.geolocation) { - navigator.geolocation.getCurrentPosition((position) => { - var coords = position.coords - if (type === 'WGS84') { - callback(coords) - } else { - wgs84ToGcj02(coords, callback, (err) => { - invoke(callbackId, { - errMsg: 'getLocation:fail ' + JSON.stringify(err) - }) - }) - } - }, () => { - invoke(callbackId, { - errMsg: 'getLocation:fail' - }) - }, { - enableHighAccuracy: altitude, - timeout: 1000 * 60 * 5 - }) - } else { + }).catch(error => { invoke(callbackId, { - errMsg: 'getLocation:fail device nonsupport geolocation' + errMsg: 'getLocation:fail ' + error.message }) - } -} + }) +}