想要在 React native 0.55.1 中获取当前位置

Want to get current location in React native 0.55.1

我想获取当前位置它适用于当前的 React Native 版本,但我想要它用于 React Native 版本 0.55.1

我这样试过 并且有效

constructor(props) {
      super(props);

      this.state = {
        latitude: null,
        longitude: null,
        error:null,
      };
    }
componentDidMount() {
      navigator.geolocation.getCurrentPosition(
         (position) => {
           console.log("wokeeey");
           console.log(position);
           this.setState({
             latitude: position.coords.latitude,
             longitude: position.coords.longitude,
             error: null,
           });
         },
         (error) => this.setState({ error: error.message }),
         { enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
       );
     }

然后我用这个打印了

<View>   
    <Text> Longitude:- {this.state.latitude} </Text>
    <Text> Latitude:- {this.state.longitude} </Text>
    <Text> {this.state.error} </Text>  
</View>

但是对于 React Native 0.55.1 它不起作用。 请帮助我,我该怎么做?

对于 android 你应该在 android 清单文件中添加下面提到的权限

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

别忘了在您的设备中启用 GPS

您也可以使用此方法获取当前位置

navigator.geolocation.watchPosition(
         (position) => {
           console.log("wokeeey");
           console.log(position);
           this.setState({
             latitude: position.coords.latitude,
             longitude: position.coords.longitude,
             error: null,
           });
         },
         (error) => this.setState({ error: error.message }),
         { enableHighAccuracy: false, timeout: 200000, maximumAge: 1000 },
       );