react-native-amap-geolocation
react-native-amap-geolocation copied to clipboard
ios getCurrentPosition 没有位置信息
android 可以正常获取,ios 则只有经纬度信息
你解决了吗
已经解决了,谢谢
------------------ 原始邮件 ------------------ 发件人: "赵亚"<[email protected]>; 发送时间: 2020年3月30日(星期一) 下午2:32 收件人: "qiuxiang/react-native-amap-geolocation"<[email protected]>; 抄送: "呐呐呐呐呐呐"<[email protected]>; "Comment"<[email protected]>; 主题: Re: [qiuxiang/react-native-amap-geolocation] ios getCurrentPosition 没有位置信息 (#111)
你解决了吗
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.
我目前是通过持续定位获取的
/**
* 获取当前位置
*/
export async function getCurrentLocation() {
try {
// android 请求权限
if (IS_ANDROID) {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
return
}
}
// 初始化 sdk
await init({
ios: '',
android: ''
})
// 监听定位变化,监听到城市位置信息之后,resolve 并停止定位
const locationPromise = new Promise(resolve => {
setLocatingWithReGeocode(true)
setNeedAddress(true)
addLocationListener(location => {
if (location && location.adCode) {
resolve(location)
stop()
}
})
})
// 超时,20 秒之后直接 resolve
const timeoutPromise = new Promise(resolve => {
setTimeout(() => {
stop()
resolve()
}, 20 * 1000)
})
start()
return Promise.race([locationPromise, timeoutPromise])
} catch (err) {
console.warn(err)
}
}