提交 6e1a07b3 编写于 作者: Q qiang

fix(h5): 优化 uni.getLocation 支持使用 IP 获取位置

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