【问题标题】:Android configuration for running react native geolocation app用于运行反应原生地理定位应用程序的 Android 配置
【发布时间】:2018-09-26 10:24:23
【问题描述】:

我正在尝试在我的基本反应应用程序中获取纬度/经度值,我尝试了以下方法:

1) 使用了 navigator.geolocation.getCurrentPosition() react 模块,但显示的是 null 值。代码如下:

class DetailsScreen extends React.Component {
constructor(props) {
super(props);

this.state = {
  latitude: null,
  longitude: null,
  error: null,
};
}

   componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      ({coords}) => {
        const {latitude, longitude} = coords
        this.setState({
          position: {
            latitude,
            longitude,
          },
          region: {
            latitude,
            longitude,
            latitudeDelta: 0.001,
            longitudeDelta: 0.001,
          }
        })
      },
      (error) => alert(JSON.stringify(error)),
      // 'enableHighAccuracy' sometimes causes problems
      // If it does, just remove it.
      {enableHighAccuracy: false} 
    );

}
    render(){
        return(
        <View style = {styles.container}>
            <Text style = {styles.boldText}>
               Latitude:
            </Text>

            <Text>
               {this.state.latitude}
            </Text>

            <Text style = {styles.boldText}>
               Longitude:
            </Text>

            <Text>
               {this.state.longitude}
            </Text>
         </View>
        )
    }

}

2) 使用 react-native-geolocation 服务获取 lat/long 值,但再次显示 null 值。这里是代码 sn-p:

class DetailsScreen extends React.Component {
constructor(props) {
super(props);
this.state = { latitude: null,
                longitude: null,
                error: null,
};

  }


componentDidMount() {
    Geolocation.getCurrentPosition(
      (position) => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        });
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: false, timeout: 30000, maximumAge: 10000 },

    );


}
render(){
        return(
        <View style = {styles.container}>
            <Text style = {styles.boldText}>
               Latitude:
            </Text>

            <Text>
               {this.state.latitude}
            </Text>

            <Text style = {styles.boldText}>
               Longitude:
            </Text>

            <Text>
               {this.state.longitude}
            </Text>
         </View>
        )
    }

}

3) 使用的 navigator.geolocation.watchCurrentPosition() 仍然没有获取 lat/long 值。

我确实继续添加了

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

在 android 模块的 manifest.xml 文件中。那么我想还有更多的 android 特定权限/更改以便获取 lat/long 值吗?请帮助。

【问题讨论】:

    标签: android react-native geolocation react-native-android geocoder


    【解决方案1】:

    回到基础,这应该可以:

           navigator.geolocation.getCurrentPosition (
                (position) => {
                    console.log("location_service", position);
                },
                (error) => { 
                    console.log(error) 
                },
                {enableHighAccuracy: false}  
            )
    

    Android 清单

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

    然后重新构建您的应用程序(不要只使用 React Native 的开发菜单中的重新加载)重新运行 react-native run-android

    添加一些内容 - 如果您遇到错误,或者它仍然没有返回,请确保您有一个“新”的位置(所以只需将您的手机移到有新 GPS 信号的地方)

    【讨论】:

      猜你喜欢
      • 2019-02-02
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 2019-12-25
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      相关资源
      最近更新 更多