Response

In iOS, there is no problem finding GPS coordinates. It works great.

On the Android side, it is unstable like iOS. This problem is both in the real device and in the emulator. Sometimes he can find a place, but sometimes not. Looking for 3 days, but did not find a solution.

When my application cannot find my location, I tried using the Google Maps application, it works like a charm.

Here is my code for iOS and Android;

getCurrentPosition() {
    navigator.geolocation.getCurrentPosition(
        (position) => {
            this.setState({ initialPosition: position });
            alert(JSON.stringify(position))
            var coords = new Coords();
            coords.Latitude = position.coords.latitude;
            coords.Longitude = position.coords.longitude;
            this.getPharmaciesByCoordinates(coords);
        },
        (error) => alert(error.message),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    );
    this.watchID = navigator.geolocation.watchPosition((position) => {
        this.setState({ initialPosition: position });
    },
        (error) => alert(error.message),
        { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
    );
}

Any solutions are welcome.

thank

+3
source share
4 answers

lib, native LocationManager. , .

; https://bitbucket.org/timhagn/react-native-google-locations#readme

android, android/app/build.gradle

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

, , Google Play Services Google Support Library Android SDK Manager, ;

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

, , . , :

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

. 2000 , " ". , , .

+3

I uninstalled maximumAge: 3600000on Android and worked

0
source

tricks if you installed

{ 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;
-1
source

Source: https://habr.com/ru/post/1672041/


All Articles