简体   繁体   English

用于运行React Native地理位置应用的Android配置

[英]Android configuration for running react native geolocation app

I am trying to fetch lat/long values in my basic react app and I tried the following ways: 我正在尝试在基本的React应用中获取经/纬度值,并且尝试了以下方法:

1) Used navigator.geolocation.getCurrentPosition() react module, but null value was displayed.Here is the code: 1)使用了navigator.geolocation.getCurrentPosition()react模块,但显示了空值,这是代码:

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) Used react-native-geolocation service to fetch the lat/long value but again null value was displayed.Here is the code snippet: 2)使用react-native-geolocation服务获取经纬度值,但再次显示空值,这是代码段:

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) Used navigator.geolocation.watchCurrentPosition() still lat/long value wasn't being fetched. 3)使用过的navigator.geolocation.watchCurrentPosition()仍未获取经纬度值。

I did go ahead and add 我确实继续添加

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

in the manifest.xml file of android module.So are there any more android specific permissions/changes I am suppose to make so that the lat/long value gets fetched??Please help. 在android模块的manifest.xml文件中。因此,我是否还要对android进行特定的权限/更改,以便获取lat / long值?请提供帮助。

Take it back to the basic, this should work: 回到基本,这应该工作:

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

Android Manifest Android清单

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

Then Rebuild your application (don't just use reload from React Native's Dev Menu) re run react-native run-android 然后重建您的应用程序(不只是使用React Native的开发菜单中的reload)重新运行react-native run-android

Some things to add - if you are getting an error, or it's still not returning make sure you have a 'fresh' location (so just move your phone to where a new GPS signal comes in) 要添加一些内容-如果遇到错误或仍然无法返回,请确保您拥有“新的”位置(因此,只需将手机移至新的GPS信号进入的位置)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM