I am trying to fetch latitiude and longitude on a button click and set it to TextInput. The issue with below code is the placeholder is always undefined.
I tried removing the value attribute, in that case the placeholder is visible but then I am unable to set the location. How can i make the placeholder visible till the location is fetched and then once location is fetched set the value to latitude.
I guess I m not using the setLatLong correctly. can we use useRef to set the values correctly.
<TextInput
style={{
borderBottomColor: "black",
borderBottomWidth: 2,
color: "black",
}}
placeholder={"Longitude"}
placeholderTextColor="blue"
editable={false}
textAlign="left"
ref={longRef}
value={latLong["latitude"] + ""}
/>
complete code is as follows
import {
Text,
View,
Button,
TouchableOpacity,
StyleSheet,
TextInput,
} from "react-native";
import RNLocation from "react-native-location";
RNLocation.configure({ distanceFilter: null });
const getLocation = () => {
let latRef = React.createRef();
let longRef = React.createRef();
let latitude;
let longitude;
let [latLong, setLatLong] = useState({});
const subscribeLocation = () => {
RNLocation.requestPermission({
ios: "whenInUse", // or 'always'
android: {
detail: "coarse", // or 'fine'
rationale: {
title: "We need to access your location",
message: "We use your location to show where you are on the map",
buttonPositive: "OK",
buttonNegative: "Cancel",
},
},
})
.then((granted) => {
console.log("Permission Granted: " + granted);
})
.catch((err) => {
console.log(err);
});
};
subscribeLocation();
const fetchLocation = () => {
if (RNLocation.getCurrentPermission()) {
RNLocation.getLatestLocation({
timeout: 60000,
}).then((location) => {
**setLatLong(location)**
});
}
};
return (
<View style={styles.container}>
<View style={styles.locationContainer}>
<TextInput
style={{
borderBottomColor: "black",
borderBottomWidth: 2,
color: "black",
}}
placeholder={"Latitude"}
placeholderTextColor="blue"
editable={false}
textAlign="left"
ref={latRef}
// value={latitude}
value={latLong["latitude"] + ""}
/>
<TextInput
style={{
borderBottomColor: "black",
borderBottomWidth: 2,
color: "black",
}}
placeholder={"Longitude"}
placeholderTextColor="blue"
editable={false}
textAlign="left"
ref={longRef}
// value=''
value={latLong["latitude"] + ""}
/>
<View style={styles.touchButton}>
<Button
title="Get Location"
color="red"
onPress={async () => {
console.log("Press");
await fetchLocation();
// loci = await fetchLocation()
// setLatLong(loci)
// console.log("Locii set")
// console.log("Lociiiiii " +loci["longitude"])
// longRef = loci["longitude"]
// latRef = loci["latitude"]
}}
/>
</View>
</View>
<View style={styles.editIdContiner}>
<View style={styles.touchButton}>
<Button
title="Get CSP Details"
color="red"
onPress={() => {
console.log("Hiii");
}}
/>
</View>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
width: "100%",
position: "absolute",
left: 0,
top: 0,
bottom: 0,
flexDirection: "column",
alignItems: "center",
justifyContent: "space-evenly",
},
locationContainer: {
width: "100%",
flex: 1,
backgroundColor: "yellow",
justifyContent: "space-evenly",
alignContent: "center",
},
editIdContiner: {
width: "100%",
flex: 1,
backgroundColor: "red",
justifyContent: "space-evenly",
alignContent: "center",
},
touchButton: {
width: "100%",
color: "grey",
alignItems: "center",
},
});
export default getLocation;
fetchLocation?? Is it an object or a string?? I am talking about thelocationvariable insidefetchLocationfunctionconsole.log(location)beforesetLatLong(location)locationshould look like this --->{ latitude: "some data", longitude: ''some data here" }fetchrequest.