React -Native Value для долготы не может быть преобразован из String в Double

Я использую react-native-maps и получаю широту и долготу, используя этот код:

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 });
  });
}

И это мой код просмотра карты:

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

Когда я вставляю этот lat и long в свой код, я получаю следующую ошибку:


person Hector4888    schedule 13.03.2019    source источник
comment
Вы никогда не добавляли ошибку в свой вопрос. Не могли бы вы включить это? Почему вы используете JSON.stringify для широты и долготы? Попробуйте удалить это и посмотрите, работает ли это.   -  person Tholle    schedule 13.03.2019


Ответы (4)


Ваша проблема в том, что вы делаете координату строкой, и она должна быть двойной.

Вы не должны использовать JSON.stringify

const currentLongitude = JSON.stringify(position.coords.longitude);
const currentLatitude = JSON.stringify(position.coords.latitude);

Вы должны сделать это вместо этого

const currentLongitude = position.coords.longitude;
const currentLatitude = position.coords.latitude;
person Andrew    schedule 13.03.2019

Нет ничего проще, чем анализировать положение координат следующим образом:

{
   latitude: parseFloat(myObj.latitude),
   longitude: parseFloat(myObj.longitude)
}
person MUSTAPHA GHLISSI    schedule 30.11.2020

Эта проблема возникает, когда ваш lat long находится в String, преобразуйте его в float.

person fazeel haider    schedule 28.12.2019

const currentLongitude = position.coords.longitude;
const currentLatitude = position.coords.latitude;

моя проблема решена с помощью приведенного выше кода

person Qusain    schedule 26.01.2020