【发布时间】:2020-04-14 22:36:33
【问题描述】:
我有一个必须返回 true 或 false 的异步函数链,但我从函数中得到未定义而不是获取位置。
这里是返回未定义的函数:
async getGeoLoc(trigger = 'cDidMount') {
return navigator.geolocation.getCurrentPosition(
async position => {
const isDataReady = await this.setCityFromCoordinate(
trigger,
position.coords.latitude,
position.coords.longitude,
);
console.log('isDataReady getGeoLoc', isDataReady); // this gives true in console
return isDataReady
},
我在这里称呼它:
async getLocationPermission(trigger) {
if (Platform.OS == 'android') {
const response = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
);
if (
response == PermissionsAndroid.RESULTS.DENIED ||
response == PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN
) {
Alert.alert(
i18n.t('search:geolocation_disabled'),
i18n.t('search:need_location'),
);
return false;
} else {
return await this.getGeoLoc(trigger);
}
} else {
return await this.getGeoLoc(trigger);
// for ios go directly here
}
},
谢谢!
【问题讨论】:
-
您没有返回
getGeoLoc中的任何内容。你得到的返回是在一个回调函数中传递给getCurrentPosition,你需要返回getCurrentPosition的结果。 -
感谢您的帮助,我已将返回添加到 getGeoLoc 但仍返回未定义。我会相应地编辑帖子
-
getCurrentPosition期望传入什么? -
你能检查一下这个reverse geocoding from LatLon我希望它的解决方案对你有用
标签: react-native asynchronous async-await