I'm using react native and try to understand some examples.
I've got a problem
I'm trying to pass latitude in my fetch but I've got a problem who say me "latitude" don't exist
class App extends React.Component {
constructor() {
super();
this.state = {
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
},
markers: [],
loaded: false
}
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
);
this.getLocations()
}
getLocations(){
return fetch('https://***&geofilter.distance='+latitude+'%2C2.3883402698750875%2C2000') //here
.then(response => response.json())
.then(responseData =>{
var markers = [];
Can you help me, thanks:)
In getLocations
, you're trying to use a latitude
variable, but there is none in scope there. You may have wanted this.state.region.latitude
.
If you're trying to use this.state.region.latitude
, though, you need to move your call to getLocations
in componentDidMount
so that it's inside a state change completion handler:
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
}
},
() => { // ***
this.getLocations(); // ***
}); // ***
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
);
// *** Not here
}
You are storing your latitude in state so you need to call it as this.state.region.latitude
return fetch('https://***&geofilter.distance='+this.state.region.latitude+'%2C2.3883402698750875%2C2000')
Try using your fetch like this:
return fetch('https://***&geofilter.distance='+this.state.region.latitude+'%2C2.3883402698750875%2C2000')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.