反应-Native“经度值不能从String转换为Double”

问题描述 投票:0回答:1

我正在使用react-native-maps,我使用此代码得到lat和long:

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代码:

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

当我在我的代码中粘贴此lat和long时出现以下错误:

reactjs react-native react-native-maps
1个回答
2
投票

您的问题是您正在使坐标成为一个字符串,并且它必须是双精度。

你不应该使用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;
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.