【问题标题】:Want to get current location in React native 0.55.1想要在 React native 0.55.1 中获取当前位置
【发布时间】:2019-05-01 11:01:17
【问题描述】:

我想获取当前位置它适用于当前的 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 它不起作用。 请帮助我该怎么办?

【问题讨论】:

    标签: react-native geolocation geocoding


    【解决方案1】:

    对于 android,您应该在 android manifest 文件中添加下面提到的权限

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

    【讨论】:

    • 谢谢这解决了我的问题。它没有添加那些行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2021-11-10
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多