React-Native Geolocation not updating (Genymotion) - android

The GPS location output from the navigator.geolocation.getCurrentPosition won't update based on the location I have set in Genymotion GPS Simulate menu. This is the code that I used to request for new location:
navigator.geolocation.getCurrentPosition(
(position) => console.log("Location: " + JSON.stringify(position)),
(error) => console.log("Error: " + error),
{enableHighAccuracy: true, timeout: 10000, maximumAge: 2000}
);
The output that I got was (see below), and the timestamp doesn't get updated.
Location: {"timestamp":1471229266000,"coords":{"speed":0,"heading":0,"accuracy":1,"longitude":0,"altitude":0,"latitude":0}}
I have added GPS Permission in the Manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
NOTE: A imperfect workaround for this issue that I am facing is to disable the GPS function on android and enable it back in order for the location to be updated based on what I have entered in the Genymotion

React native getCurrentPosition uses cached position depending on your maximumAge value. Try setting maximumAge to 0

Related

Bad/poor GPS location accuracy on Android with React Native

I am working on a React Native app that works both on Android and iPhone. I have a feature where you can "share my current location". This works very well and accurately on iPhone but on Android, it only shows the correct city/locality however the actual location is far off from your street. For getting the location I use the https://github.com/Agontuk/react-native-geolocation-service library.
Here's the relevant code, I'm simply calling Geolocation.getCurrentPosition, not doing anything special. And I'm asking for high accuracy in options. Any ideas on how I can improve the accuracy on Android?
Edit: If I open Google Maps on the same Android phone, I get the accurate/correct location.
Geolocation.getCurrentPosition(
(position) => {
// Do stuff with the position
},
(error) => {
// See error code charts below.
logError(
new Error(
`Cannot get current location: ${error.code} ${error.message}`,
),
);
},
{
enableHighAccuracy: true,
accuracy: {
android: 'high',
ios: 'bestForNavigation',
},
timeout: 15000,
maximumAge: 10000,
distanceFilter: 10,
},
);
}
},```
I figured out that the issue was that I did not have the ACCES_FINE_LOCATION permission in my AndroidManifest.xml. It's a bit confusing because this did not result in any actual error, it just did not give accurate location instead.
So just add this to the manifest file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

React-native app's location request times out when in background (emulator)

I'm using navigator.geolocation.getCurrentPosition to get coordinates in my app.
Initially, my app allows location services as specified in AndroidManifest.xml through:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
And location services works fine. Then, I go to the emulator's settings, which puts the app in the background, turn the location off, and go back to the app where it detects that location has indeed been off, so it prompts me and lets me enable location through settings again, using this code:
if (Platform.OS === 'android') {
LocationServicesDialogBox.checkLocationServicesIsEnabled({
message: "<h2>Use Location ?</h2>",
ok: 'YES',
cancel: 'NO',
enableHighAccuracy: false,
showDialog: true,
openLocationServices: true,
}).then((success) => {
console.log(success);
}).catch((error) => {
console.log(error.message);
});
}
And when I click 'OK' on the prompt if I should allow the app for location permission, it now says 'location request timed out'. And this is my getCurrentLocation code:
handleRefreshLocation() {
console.log('refreshing location');
navigator.geolocation.getCurrentPosition(
(position) => {
console.log('location refreshed');
this.setState({
long: position.coords.longitude,
lat: position.coords.latitude,
error: null,
}, this.updateLocation); //updateLocation is an API call to update DB
},
error => this.setState({ error: error.message }, this.printError),
{ enableHighAccuracy: false, timeout: 20000 },
);
}
I've already tried setting enableHighAccuracy to false, or removing the third argument of getCurrentPosition altogether, but nothing works. Is there any other fix to this? Thanks!
Have you tried removing just the enableHighAccuracy parameter and leaving the timeout?
Also try adding this to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

react native navigator.geolocation.getCurrentPosition not working

I am using a real android device (version 5.1)
I am able to determine my position using react-native-maps (correct blue point position on map inside my app ) + I am able use google maps app and go to my position (GPS works).
I am using a big timeout, toggling enableHighAccuracy to true and false, remove options ... etc . all failed to get navigator.geolocation to get data.
Here is my code:
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function success(pos) {
var crd = pos.coords;
console.log('Your current position is:');
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude: ${crd.longitude}`);
console.log(`More or less ${crd.accuracy} meters.`);
};
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
};
navigator.geolocation.getCurrentPosition(success, error, options);
I am getting : ERROR(3): Location request timed out
I know it's too late, but it can help someone else.
Geolocation has been extracted from react native .60 version. If you need an alternative solution
Install react-native-community/geolocation :
npm install #react-native-community/geolocation --save
react-native link #react-native-community/geolocation
Then, to request access to location, you need to add the following line to your app's AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Usage :
import Geolocation from '#react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));
Two things.
1) That is actually not a high timeout for a high accuracy response if the coordinates are not cached
2) Some devices have problems with the highAccuracy setting.
Try putting 30000 ms as timeout and remove the high accuracy until you find one setting that works.
Edit: I found the long bug I was remembering form React Native: https://github.com/facebook/react-native/issues/7495
So, try this:
1) Remove maximumAge property
2) If that does not work, remove the third parameter and use the default values from the native module. Meaning, don't send the options object. That should work.
Removing maximumAge solved the issue here! Just in case someone is still having this problem in 2019 or later
To request access to location, you need to add the following line to your app's AndroidManifest.xml: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />.
For an android with API >= 23 you need to require an additional step to check for: you need to request the ACCESS_FINE_LOCATION permission using the PermissionsAndroid API.
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: "Location Accessing Permission",
message: "App needs access to your location"
}
);
And only after that run navigator.geolocation.getCurrentPosition(success, error, options);
I used this and it works fine: {enableHighAccuracy: false, timeout: 50000}
async watchPosition() {
console.log('watchPosition');
await navigator.geolocation.getCurrentPosition(
(position) => {
console.log('Position -> ',position);
},
(error) => console.log(error)
{enableHighAccuracy: false, timeout: 50000}
);
}

navigator.geolocation.getCurrentPosition always getting timeout in Android until GPS/Location is OFF

app.controller('dashboard', function($scope){
$scope.getPosition = function(position){
$scope.acc = position.coords;
$scope.lat = position.coords.latitude;
$scope.lng = position.coords.longitude;
$scope.$apply();
};
$scope.getPositionErr = function(error){
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
};
navigator.geolocation.getCurrentPosition($scope.getPosition, $scope.getPositionErr, {maximumAge: 0, timeout: 6000, enableHighAccuracy:false});
});
I am using Angular JS and Cordova for an Android app.
This code is working fine on desktop but not on Nexus 4 with Lollipop.
Also controller code is executing after deviceready as per requirement from Cordova
I have tried
Setting HighAccuracy to false
Removing geolocation plugin so that it uses default location from Wifi
Device reboot
Clearing browser cache
Airplane mode on/off
Wifi on/off
Mobile data on/off
But I am unable to get lat, long and geolocation always gets timeout.
It only works when I enabled Location/GPS from setting. After enabling it code is working as expected.
navigator.geolocation.getCurrentPosition is only for GPS when option "enableHighAccuracy" is set as 'true'(default). That's why you are not able to get current location via mobile device. About "it is working on desktop", I believe you must have opened GPS setting in your browser.
Usually I used code below t o get position no matter GPS is open or not.
var positionOption = { timeout: 500, enableHighAccuracy: true };
var gpsSunccuss = function(currentPosition) {
//use gps position
};
var gpsFailed = function() {
//use some 3rd party position solution(get position by your device ip)
getPositionBy3rdParty();
};
navigator.geolocation.getCurrentPosition(gpsSunccuss, gpsFailed, positionOption);
On the other hand, you could set enableHighAccuracy as 'false'.
I had identical issue and this fixed it for me
{enableHighAccuracy:false,maximumAge:Infinity, timeout:60000}
I think setting maximumAge to Infinity only works because in that case it just gives you back the previous location when the timeout occurs. I wouldn't consider that working.
you can try this.
{enableHighAccuracy:true, Infinity:Infinity, timeout:2000}

PhoneGap Geolocation get always a timeout on special devices

I use cordova 2.9.0 with PhoneGap Build. I have written an app where a user can check-in on special location to a special time.
My problem is that when I install de app on various devices I sometimes get always a timout (this occurs only on Android devices). When I restart the device the geolocation works and I get the gps-data. Now I'd like to know if there is another way to resolve this problem.
My code for geolocation in the deviceReady function is:
var geo = cordova.require('cordova/plugin/geolocation');
var optionsGeo = {maximumAge: 0, timeout: 30000, enableHighAccuracy: false};
var watchID = geo.watchPosition(onSuccessGeo, errorGeo, optionsGeo);
function onSuccessGeo(position) {
lat = (position.coords.latitude).toFixed(6);
lon = (position.coords.longitude).toFixed(6);
accuracy = (position.coords.accuracy).toFixed(0);
console.log("Lat " + lat + " Lon " + lon + " & " + accuracy + "m");
}
function errorGeo(error) {
console.log("Geo-Fehler! Code: " + error.code + " Nachricht: " + error.message);
}
I have tried with different timeoutvalues and with enabled HighAccuracy - but nothing helps.
Thanks.
I was also facing this issue, mainly with Samsung android devices. What I found out is that:
If you do not have a SIM card installed in the device, then there will be no gps coordinates (that's only applicable to some devices).
Please make sure that you have <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> set in your android manifest.
You should set enableHighAccuracy:true please see below my settings:
var options = {maximumAge: 0, timeout: 10000, enableHighAccuracy:true};
navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
enableHighAccuracy provides a hint that the application would like to receive the best possible results. By default, the device will attempt to retrieve a Position using network-based methods. Setting this property to true tells the framework to use more accurate methods, such as satellite positioning.
In some cases, developers use geolocation.watchPosition to get more accurate gps results. Can you also try that?
I hope it helps!
I know this question is old but I encountered the same problem today using Cordova 3.5.0 with the geolocation plugin (org.apache.cordova.geolocation 0.3.8).
I solved it by enabling the GPS "HighAccuracy"-mode on the device (android). (Settings -> Location access -> Mode).
The app-user gets a hint that he needs to enable this option in order to use the app...

Categories

Resources