دنبال کننده ها

۱۳۹۶ مهر ۱۰, دوشنبه

ReactNative GeoLocation with another asynchronous api call inside not working

[ad_1]



I am using geolocation to get the current coordinates (latitude and longitude). When the coordinates are available, I want to be able to make an API call using the returned coordinates.
However, the API call is not executing right for some reason. What is the right way to make the API call right after geolocation has returned the coordinates ?
Here is the code :



//Getting the current location using the geolocation API
navigator.geolocation.getCurrentPosition(
(position) =>
this.setState(
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null
);


//Here is the API call to be executed after the coordinates are returned and state is set for the latitude and longitude

const url = 'http://sewoa-easy-breezy-home-number-api-us1.smx.online/api/v1/houses/number?latitude=' + that.state.latitude
+ '&longitude=' + that.state.longitude;
axios.get(url).then(function (response)
if (response.data)
Actions.viewHouseNumber( houseNumber: response.data.number );
else
Actions.houseNotFound( latitude: that.state.latitude, longitude: that.state.longitude );

).catch(() =>
// The request was made, but the server responded with a status code
// that falls out of the range of 2xx
if (that.state.latitude && that.state.longitude)
Actions.houseNotFound( latitude: that.state.latitude, longitude: that.state.longitude );

else
Actions.error( message: 'connection_error');

);
,
(error) =>
Actions.error( message: 'gps_error' );
return;
,
enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 ,
)


Both navigation.getCurrentPostion and axios.get methods used here work on a separately. However now that the API call is inside the gelocation.getCurrentPosition method, the whole thing is not working. Any idea on how I could solve this ?




[ad_2]

لینک منبع