英文:
Several Interesting Problem on React Native
问题
以下是您要翻译的内容:
"I want to share on of my react native project. Here I am getting some issues. I have tried to solve that many time, but failed. Can anyone fix that?
I have checked all the questions asked on stack overflow. I can assure that this a really new problem.
Errors: 1. In Location.Address it is showing that address module is not loaded.
-
Location.requestPermissionsAsync() not working. Showing a cross over the code.
-
In let {status} it is showing an error.
Please Note I have written this code in a .tsx file."
import React, { useState, useReducer, useEffect } from "react";
import { View, Text, StyleSheet, Image, Dimensions, TouchableOpacity } from "react-native";
import * as Location from "expo-location";
const screenWidth = Dimensions.get("screen").width;
export const LandingScreen = () => {
const [errorMsg, setErrorMsg] = useState("");
const [address, setAddress] = useState<Location.Address>();
const [displayAddress, setDisplayAddress] = useState("Waiting for current location");
useEffect(() => {
(async () => {
let { status } = await Location.requestPermissionsAsync();
if (status !== "granted") {
setErrorMsg("permissions not granted, Allow the app to use location service");
}
let location: any = await Location.getCurrentPositionAsync();
const { coords } = location;
if (coords) {
const { latitude, longitude } = coords;
let addressResponse: any = await Location.reverseGeocodeAsync({
latitude,
longitude,
});
for (let item of addressResponse) {
setAddress(item);
let currentAddress = `${item.name}, ${item.street}, ${item.postalCode}, ${item.city}`;
setDisplayAddress(currentAddress);
}
}
});
}, []);
return (
<View style={styles.container}>
<View style={styles.navigation} />
<View style={styles.body}>
<Image source={require("../images/delivery.png")} style={styles.deliveryIcon} />
<View style={styles.addressContainer}>
<Text style={styles.addressTitle}>Your deliver address</Text>
</View>
<Text style={styles.addressText}>{displayAddress}</Text>
</View>
<View style={styles.footer} />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'rgba(242, 242, 242, 1)',
},
navigation: {
flex: 2,
},
body: {
flex: 9,
justifyContent: "center",
alignItems: "center",
},
footer: {
flex: 1,
},
deliveryIcon: {
width: 120,
height: 120,
},
addressContainer: {
width: screenWidth - 100,
borderBottomColor: "red",
borderBottomWidth: 0.5,
padding: 5,
marginBottom: 10,
alignItems: "center",
},
addressTitle: {
fontSize: 24,
fontWeight: "700",
color: "#7D7D7D",
},
addressText: {
fontSize: 20,
fontWeight: "200",
color: "#4F4F4F",
},
});
英文:
I want to share on of my react native project. Here I am getting some issues. I have tried to solve that many time, but failed. Can anyone fix that?
I have checked all the questions asked on stack overflow. I can assure that this a really new problem.
Errors: 1. In Location.Address it is showing that address module is not loaded.
2.Location.requestPermissionsAsync() not working. Showing a cross over the code.
- In let {status} it is showing an error.
Please Note I have written this code in a .tsx file.
import React, {useState, useReducer,useEffect} from "react"
import {View, Text, StyleSheet, Image, Dimensions, TouchableOpacity} from "react-native"
import * as Location from "expo-location"
const screenWidth = Dimensions.get("screen").width
export const LandingScreen = () => {
const[errorMsg, setErrorMsg] = useState("")
const [address, setAddress] = useState<Location.Address>()
const [displayAddress, setDisplayAddress] = useState("Waiting for current location")
useEffect(() =>{
(async () =>{
let { status } = await Location.requestPermissionsAsync()
if(status !== "granted"){
setErrorMsg(
"permissions not granted ,Allow the app to use location service"
)
}
let location: any = await Location.getCurrentPositionAsync()
const {coords} = location
if(coords){
const{latitude, longitude} = coords;
let addressResponse: any = await Location.reverseGeocodeAsync({
latitude,
longitude
});
for(let item of addressResponse){
setAddress(item)
let currentAddress = `${item.name}, ${item.street}, ${item.postalCode}, ${item.city}`
setDisplayAddress(currentAddress)
/*if(address.length > 0){
setTimeout(()=>{
navigate("HomeScreen")
},1000)
}*/
}
}
})
},[])
return(
<View style={styles.container}>
<View style={styles.navigation}/>
<View style={styles.body}>
<Image source={require("../images/delivery.png")} style={styles.deliveryIcon}/>
<View style={styles.addressContainer}>
<Text style={styles.addressTitle}>Your deliver address</Text>
</View>
<Text style={styles.addressText}>{displayAddress}</Text>
</View>
<View style={styles.footer}/>
</View>
)
}
const styles = StyleSheet.create({
container:{
flex:1,
backgroundColor:'rgba(242,242,242,1)'
},
navigation:{
flex:2,
},
body:{
flex:9,
justifyContent:"center",
alignItems:"center",
},
footer:{
flex:1,
},
deliveryIcon:{
width:120,
height:120
},
addressContainer:{
width:screenWidth - 100,
borderBottomColor:"red",
borderBottomWidth:0.5,
padding:5,
marginBottom:10,
alignItems:"center"
},
addressTitle:{
fontSize:24,
fontWeight:"700",
color:"#7D7D7D"
},
addressText:{
fontSize:20,
fontWeight:"200",
color:"#4F4F4F"
}
})
答案1
得分: 0
-
用
let { status } = await Location.requestForegroundPermissionsAsync();
代替let { status } = await Location.requestPermissionsAsync();
-
删除这部分
<Location.Address>
,应该是const [address, setAddress] = useState()
。
英文:
- Insted of
let { status } = await Location.requestPermissionsAsync()
use
let { status } = await Location.requestForegroundPermissionsAsync();
2.
remove this <Location.Address>
it should be const [address, setAddress] = useState()
答案2
得分: 0
-
正如 @komal-harmale 提到的,使用
[address, setAddress] = useState()
。 -
在 Location.Address 中显示地址模块未加载。
Location.requestPermissionsAsync() 已被弃用。https://docs.expo.dev/versions/latest/sdk/location/#locationrequestpermissionsasync
在这种情况下,你需要使用 requestForegroundPermissionsAsync 或 requestBackgroundPermissionsAsync 来代替它。我也遇到了相同的情况,我通过使用 requestForegroundPermissionsAsync、requestBackgroundPermissionsAsync 和 Modal 来解决了这个问题。我会简要解释我做了什么。
- 在你想要请求权限的地方初始化这些状态变量。
const [foreground, requestForeground] = ExpoLocation.useForegroundPermissions(); const [background, requestBackground] = ExpoLocation.useBackgroundPermissions();
- 使用前面提到的状态对象创建一个 Modal。以下是一个从用户那里请求权限的示例 Modal。
import { Button, Linking, Modal, StyleSheet, Text, View } from "react-native"; export const LocationRequestModal = ({ foreground, requestForeground, background, requestBackground }) => { return ( <Modal visible={foreground && !foreground.granted && background && !background.granted}> <View style={Styles.backDrop}> <View style={Styles.middleView}> <View style={{ marginVertical: 16 }}> <Text style={{ fontSize: 20, fontWeight: "bold" }}>请授予权限。</Text> </View> <View style={{ marginVertical: 16 }}> <Text style={{ fontSize: 18 }}>前台权限:</Text> <Text>状态: {foreground?.status || "等待"}</Text> {foreground && !foreground.granted && foreground.canAskAgain && <Button title="授予权限" onPress={requestForeground} />} {foreground && !foreground.granted && !foreground.canAskAgain && ( <Button title="通过设置授予权限" onPress={() => requestForeground().then((p) => !p.granted && Linking.openSettings())} /> )} </View> <View style={{ marginVertical: 16 }}> <Text style={{ fontSize: 18 }}>后台权限:</Text> <Text>状态: {background?.status || "等待"}</Text> {!foreground?.granted && <Text>首先授予前台位置权限</Text>} {foreground?.granted && background && !background.granted && background.canAskAgain && ( <Button title="授予权限" onPress={requestBackground} /> )} {foreground?.granted && background && !background.granted && !background.canAskAgain && ( <Button title="通过设置授予权限" onPress={() => requestBackground().then((p) => !p.granted && Linking.openSettings())} /> )} </View> </View> </View> </Modal> ); }; const Styles = StyleSheet.create({ backDrop: { backgroundColor: "rgba(52, 52, 52, 0.8)", height: "100%", width: "100%", paddingBottom: "3%", paddingTop: "3%", paddingRight: "6%", paddingLeft: "6%", justifyContent: "center", alignItems: "center", }, middleView: { height: "60%", width: "60%", backgroundColor: "white", justifyContent: "center", alignItems: "center" }, });
英文:
- as @komal-harmale mentioned use the
[address, setAddress] = useState()
- In Location.Address it is showing that address module is not loaded.
<br><br>
Location.requestPermissionsAsync() has been deprecated. https://docs.expo.dev/versions/latest/sdk/location/#locationrequestpermissionsasync
<br>In that case you have to use In that case you have to use requestForegroundPermissionsAsync or requestBackgroundPermissionsAsync instead of that.
<br>
<br>
Same scenario happened to me and I overcame that by using requestForegroundPermissionsAsync, requestBackgroundPermissionsAsync and with a help of Modal. will explain briefly what I did.
<br>
<br>
1 . initialize these states variables where you wants to request permissions.
const [foreground, requestForeground] = ExpoLocation.useForegroundPermissions();
const [background, requestBackground] = ExpoLocation.useBackgroundPermissions();
<br><br>
2 . Create a Modal with the use of earlier mentioned state objects. Here is a sample Modal to request permissions from user. <br>
import { Button, Linking, Modal, StyleSheet, Text, View } from "react-native";
export const LocationRequestModal = ({ foreground, requestForeground, background, requestBackground }) => {
return (
<Modal visible={foreground && !foreground.granted && background && !background.granted}>
<View style={Styles.backDrop}>
<View style={Styles.middleView}>
<View style={{ marginVertical: 16 }}>
<Text style={{ fontSize: 20, fontWeight: "bold" }}>Pleae grant permission.</Text>
</View>
<View style={{ marginVertical: 16 }}>
<Text style={{ fontSize: 18 }}>Foreground permission:</Text>
<Text>status: {foreground?.status || "pending"}</Text>
{foreground && !foreground.granted && foreground.canAskAgain && <Button title="Grant permission" onPress={requestForeground} />}
{foreground && !foreground.granted && !foreground.canAskAgain && (
<Button
title="Grant permission through settings"
onPress={() => requestForeground().then((p) => !p.granted && Linking.openSettings())}
/>
)}
</View>
<View style={{ marginVertical: 16 }}>
<Text style={{ fontSize: 18 }}>Background permission:</Text>
<Text>status: {background?.status || "pending"}</Text>
{!foreground?.granted && <Text>Grant foreground location permission first</Text>}
{foreground?.granted && background && !background.granted && background.canAskAgain && (
<Button title="Grant permission" onPress={requestBackground} />
)}
{foreground?.granted && background && !background.granted && !background.canAskAgain && (
<Button
title="Grant permission through settings"
onPress={() => requestBackground().then((p) => !p.granted && Linking.openSettings())}
/>
)}
</View>
</View>
</View>
</Modal>
);
};
const Styles = StyleSheet.create({
backDrop: {
backgroundColor: "rgba(52, 52, 52, 0.8)",
height: "100%",
width: "100%",
paddingBottom: "3%",
paddingTop: "3%",
paddingRight: "6%",
paddingLeft: "6%",
justifyContent: "center",
alignItems: "center",
},
middleView: { height: "60%", width: "60%", backgroundColor: "white", justifyContent: "center", alignItems: "center" },
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论