为什么反应本机地图初始区不起作用?
当我介绍我的地图时,它永远不会从某种程度上加载初学,它总是从非洲地区开始。 GIF中可见的加载来自API调用,当Get请求是成功的时候,我将其停止。
这是我的代码:
const [mapRegion, setMapRegion] = useState(null);
const getLocation = async ()=>{
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
Alert.alert(
'Whoops!',
'Az alkalmazás nem kapott hely információ engedélyt. Ezt könnyen orvosolhatod telefonod beállításiban. \n\nSzeretnéd most bekapcsolni?',
[
{text: 'Igen', onPress: () => openSettings()},
{text: 'Nem', style: 'cancel'},
],
{ cancelable: true }
)
return;
}
let location = await Location.getCurrentPositionAsync({accuracy: Location.Accuracy.Highest, maximumAge: 10000}); //accuracy: Location.Accuracy.Highest, maximumAge: 10000
setUserPosition(location);
let mapRegion = {
latitude:location.coords.latitude,
longitude:location.coords.longitude,
latitudeDelta: .01,
longitudeDelta: .01}
setMapRegion(mapRegion)
}
useEffect(() => {
getLocation()
}, []);
return(
<View style={styles.container}>
<MapView style={styles.map}
provider={PROVIDER_GOOGLE}
region={mapRegion}
initialRegion={{latitude:47.090417,longitude:19.234115,latitudeDelta: 5,longitudeDelta: 5}}
showsUserLocation={true}
showsCompass={true}
>
</MapView>
</View>
)
When I present my map it never loads the initRegion somewhy, it always starts with the african region. The loading that is visible in the gif is coming from an API call, and I stop it when the get request was succesfull.
Heres my code:
const [mapRegion, setMapRegion] = useState(null);
const getLocation = async ()=>{
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
Alert.alert(
'Whoops!',
'Az alkalmazás nem kapott hely információ engedélyt. Ezt könnyen orvosolhatod telefonod beállításiban. \n\nSzeretnéd most bekapcsolni?',
[
{text: 'Igen', onPress: () => openSettings()},
{text: 'Nem', style: 'cancel'},
],
{ cancelable: true }
)
return;
}
let location = await Location.getCurrentPositionAsync({accuracy: Location.Accuracy.Highest, maximumAge: 10000}); //accuracy: Location.Accuracy.Highest, maximumAge: 10000
setUserPosition(location);
let mapRegion = {
latitude:location.coords.latitude,
longitude:location.coords.longitude,
latitudeDelta: .01,
longitudeDelta: .01}
setMapRegion(mapRegion)
}
useEffect(() => {
getLocation()
}, []);
return(
<View style={styles.container}>
<MapView style={styles.map}
provider={PROVIDER_GOOGLE}
region={mapRegion}
initialRegion={{latitude:47.090417,longitude:19.234115,latitudeDelta: 5,longitudeDelta: 5}}
showsUserLocation={true}
showsCompass={true}
>
</MapView>
</View>
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,如果您在代码中使用某种加载,那么 initRegion 将不起作用:
之前:
之后:
Turns out if you use some kind of loading in your code, then initRegion does not work:
Before:
After: