React Native Android location request timed out

坚强是说给别人听的谎言 提交于 2019-12-03 22:32:20

I removed maximumAge: 3600000 on Android and it working

It's possible that you are inside of the building or office and your signal is not that good. I've tried with many configuration and this one fit the best for me:

{
    enableHighAccuracy: false,
    timeout: 5000,
    maximumAge: 10000
}

Or try increasing timeout. Because for me the usual 2000ms wasn't working, I kept getting "Location request timed out". So disable high accuracy, increase timeout and you're good to go.

Hi I was facing same issue "request time out ". I have tried every possible solution which I found but, those could not work for me but recently I found one module which overcomes this problem. react-native-geolocation-service This worked for me .....

I found a solution via external lib that uses native LocationManager. It uses play services location and emitting location event as I want.

Here is the library; https://bitbucket.org/timhagn/react-native-google-locations#readme

And when compiling for android, don't forget to include android/app/build.gradle

dependencies {
    ...
    compile "com.google.android.gms:play-services:10.0.1" -> which version that you have write down here.
    ...
}

And finally don't forget to download Google Play Services and Google Support Library in Android SDK Manager you can find your play services versions into;

<android-sdk-location>/<sdk-version>/extras/google/m2repository/com/google/android/gms/play-services

I have done the app by location request. The command
{ enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 }, will work on iOS but not in Android. I think you should check Platform.

navigator.geolocation.getCurrentPosition(
          (location) => {
            console.log('location ', location);
            if (this.validLocation(location.coords)) {
              this.locationToAddress(location.coords);
            }
          },
          (error) => {
            console.log('request location error', error);
          },
          Platform.OS === 'android' ? {} : { enableHighAccuracy: true, timeout: 20000, maximumAge: 10000  }
        );

I tried various methods and npm modules. Finally I got solved this issue by using this module. I strongly suggest this. https://www.npmjs.com/package/react-native-geolocation-service

Working { enableHighAccuracy: false, timeout: 5000, maximumAge: 10000 },

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

Add Permission in P list and android Manifest also

import React, { Component } from 'react';
import { View, Text } from 'react-native';

export default class App extends Component {
  constructor(props) {
    super(props);

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

  componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        });
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: false, timeout: 5000, maximumAge: 10000 },
      // { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
    );
  }

  render() {
    return (
      <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Latitude: {this.state.latitude}</Text>
        <Text>Longitude: {this.state.longitude}</Text>
        {this.state.error ? <Text>Error: {this.state.error}</Text> : null}
      </View>
    );
  }
}

the tricks is, if you set

{ enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 } 

then it will work. full code below.

import React, { Component } from 'react';
import { View, Text } from 'react-native';

class GeolocationExample extends Component {
constructor(props) {
super(props);

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

componentDidMount() {
navigator.geolocation.getCurrentPosition(
  (position) => {
    this.setState({
      latitude: position.coords.latitude,
      longitude: position.coords.longitude,
      error: null,
       });
       },
  (error) => this.setState({ error: error.message }),
  { enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 },
);}

 render() {
return (
  <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
    <Text>Latitude: {this.state.latitude}</Text>
    <Text>Longitude: {this.state.longitude}</Text>
    {this.state.error ? <Text>Error: {this.state.error}</Text> : null}
  </View>
  );
 }
}

export default GeolocationExample;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!