世博会地点在ios中不工作,并且在应用程序设置中未显示地点权限
我试图在IOS 14中获取当前位置,但没有收到任何响应,当我签入expo时 设置它没有显示位置权限。我已经检查了模拟器和物理设备 钩码世博会地点在ios中不工作,并且在应用程序设置中未显示地点权限,ios,react-native,expo,Ios,React Native,Expo,我试图在IOS 14中获取当前位置,但没有收到任何响应,当我签入expo时 设置它没有显示位置权限。我已经检查了模拟器和物理设备 钩码 import { useEffect, useState } from "react"; import * as Location from "expo-location"; export default useLocation = () => { const [location, setLocation] =
import { useEffect, useState } from "react";
import * as Location from "expo-location";
export default useLocation = () => {
const [location, setLocation] = useState();
const getLocation = async () => {
try {
const { granted } = await Location.requestPermissionsAsync();
if (!granted) return;
const {
coords: { latitude, longitude },
} = await Location.getLastKnownPositionAsync();
setLocation({ latitude, longitude });
} catch (error) {
console.log(error);
}
};
useEffect(() => {
getLocation();
}, []);
return location;
};
回应
undefined
Location.getLastKnownPositionAsync()
可能返回空值:
返回解析为LocationObject或类型的对象的承诺
如果它不可用或与给定的要求不匹配,则为null,例如
最大年龄或要求的准确度
所以你应该做一些类似的事情:
import { useEffect, useState } from "react";
import * as Location from "expo-location";
export default useLocation = () => {
const [location, setLocation] = useState();
const getLocation = async () => {
try {
const { granted } = await Location.requestPermissionsAsync();
if (!granted) return;
const last = await Location.getLastKnownPositionAsync();
if (last) setLocation(last);
else {
const current = await Location.getCurrentPositionAsync();
setLocation(current);
}
} catch (error) {
console.log(error);
}
};
useEffect(() => {
getLocation();
}, []);
return location;
};
我也有这个问题。莫什的密码?我无法使该方法正常工作,但Location.getCurrentPositionAsync()方法似乎正常工作。有趣的是,我也遇到了同样的问题。知道我的代码没有问题感觉很好。这个问题已经为我解决了,我正在使用Location.getCurrentPositionAsync,我已经卸载了应用程序,然后重新启动了物理设备,然后再次安装。它可以工作:)