Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React -Native "Value for longitude cannot be cast from String to Double"

I am using react-native-maps and I get lat and long using this code:

callLocation(that) {
  navigator.geolocation.getCurrentPosition(
    position => {
      const currentLongitude = JSON.stringify(position.coords.longitude);
      const currentLatitude = JSON.stringify(position.coords.latitude);
      that.setState({ currentLongitude: currentLongitude });
      that.setState({ currentLatitude: currentLatitude });
    },
    error => alert(error.message),
    { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
  );
  that.watchID = navigator.geolocation.watchPosition(position => {
    console.log(position);
    const currentLongitude = JSON.stringify(position.coords.longitude);
    const currentLatitude = JSON.stringify(position.coords.latitude);
    that.setState({ currentLongitude: currentLongitude });
    that.setState({ currentLatitude: currentLatitude });
  });
}

And this is my mapview code:

<MapView
  style={styles.map}
  initialRegion={{
    latitude: this.state.currentLatitude,
    longitude: this.state.currentLongitude,
    latitudeDelta: 0.0922,
    longitudeDelta: 0.0421
  }}
/>

When I paste this lat and long in my code I got following error:

like image 686
Hector4888 Avatar asked Jan 19 '26 22:01

Hector4888


1 Answers

Nothing to do easier than parsing the coords position like this:

{
   latitude: parseFloat(myObj.latitude),
   longitude: parseFloat(myObj.longitude)
}
like image 109
Mustapha GHLISSI Avatar answered Jan 22 '26 14:01

Mustapha GHLISSI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!