Geo-location watch only fires every minute. Using a Nexus-5, android 4.4.2, and cordova 3.3.1 (I've also tried 3.4.0). It makes no difference what I define maximumAge as. The location returned is correct. In other devices I have have tested it gets fired every second. I know I can fall back on using setTimeout with getCurrentPosition, but I'd like to understand this behaviour.
navigator.geolocation.watchPosition(
function(){
console.log("success");
},
function(){
console.log("fail");
},
{
enableHighAccuracy: true,
timeout: 30000
}
);
The Geolocation Plugin is not that great, and will actually be deprecated in the next Cordova release. They suggest that you just use the HTML5 Geolocation API instead, which uses the same syntax.
Related
Update: it is a Google Play Service issue, reported internally here and it will be fixed from version 13.4.0
We use the cordova gelocation plugin and the method navigator.geolocation.watchPosition() with the option enableHighAccuracy: true to track the user location and get the most accurate results.
Our app has been around for more than 1 year and we used to have no problems with any device getting a very good location accuracy, 4/6 meters when outside and the sky is clear.
Lately, many of our users are reporting not being able to get anything less than 10m accuracy no matter what they do.
We decided to test it ourselves and we found to have the same issue. Initially, we thought we introduced some bug in our latest release, we triple checked everything but we made no changes to code/dependencies involving geolocation.
We tested older versions of our app as well, where we are sure it was possible to get like 4m accuracy, but surprisingly they do not work either, accuracy is capped at 10m.
We tried different version of Android and we can reproduce the issue on any platform from 5 (Lollipop) to 8 (Oreo). We also have the same problem on iOS 10/11. Again, we have not updated the app in months.
There is a recent question about the same issue here:
Someone else is having the same problem using Android native code here
Does anyone know what is going on? Is it a permission issue? Location Services are set to High Accuracy as well.
For the sake of completeness, we are able to get 3/4 meters accuracy using the old version (2.x) of this plugin
Is it the only way to go?
We would rather not introduce an extra dependency for something that was working so well out of the box.
Many thanks
Looking at source code:
Old plugin (2.x) Source:
watchPosition: function(success, error, args) {
var win = function() {
var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation');
geo.watchPosition(success, error, {
enableHighAccuracy: args[1]
});
};
exec(win, error, "Geolocation", "getPermission", []);
},
New Plugin (master) Source:
watchPosition: function(success, error, args) {
var pluginWatchId = utils.createUUID();
var win = function() {
var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation');
pluginToNativeWatchMap[pluginWatchId] = geo.watchPosition(success, error, args);
};
var fail = function() {
if (error) {
error(new PositionError(PositionError.PERMISSION_DENIED, 'Illegal Access'));
}
};
exec(win, fail, "Geolocation", "getPermission", []);
return pluginWatchId;
},
In OLD plugin code enableHighAccuracy is a boolean set by (arg1 of array).
With NEW version of plugin you need to pass arg as JSON with that flag set: {enableHighAccuracy: true} to reproduce same call to geo.watchPosition function with high accuracy.
Old Way:
navigator.geolocation.watchPosition(geolocationSuccess,
geolocationError,
[false,true]);
New Way:
navigator.geolocation.watchPosition(geolocationSuccess,
geolocationError,
{ enableHighAccuracy: true });
To whom it might concern, it is a Google Play Services issue, reported internally here and it will be fixed from version 13.4.0
Update: solved after updating to Play Services 14.3.66, accuracy down to 4m again!
I am developing an mobile application (platform: Android, iOS) with Cordova.
My application needs ping an URL to fetch data every hours. I want my application still ping the URL when it's closed.
I searched in google and I get some of this plugins:
https://github.com/katzer/cordova-plugin-local-notifications
https://github.com/katzer/cordova-plugin-background-mode
I need a plugin like the second one but also work when the application is closed like scheduled notification like the first one.
Is there any plugin like this for cordova? Or it's impossible to do background task like this with cordova.
Thank you
I had some what the same issue, i needed to pick lat,lng every few minutes and calcuate the distance, but background plugin alone couldnt solve it, as it stops working when the phone goes to sleep .. so I had to make sure, the phone doesn't go to sleep ..
So i used power management plugin together with Background mode plugin.. and it works well..
Background mode Plugin:
https://github.com/katzer/cordova-plugin-background-mode
Power Management Plugin
https://github.com/boltex/cordova-plugin-powermanagement
if( ionic.Platform.isAndroid() ){
cordova.plugins.backgroundMode.enable();
window.powerManagement.dim(function() {
console.log('Wakelock acquired');
}, function() {
console.log('Failed to acquire wakelock');
});
window.powerManagement.setReleaseOnPause(false, function() {
console.log('setReleaseOnPause successfully');
}, function() {
console.log('Failed to set');
});
}
i'm trying to get the geolocation on Android Browser but nothing happens. I'm using a Samsung Galaxy S3 but i'm not sure about the version of my browser. Android version is 4.1.2
Here is my Code:
if (navigator.geolocation) {
var timeoutVal = 10 * 1000 * 1000;
navigator.geolocation.getCurrentPosition(
displayPosition,
displayError,
{ enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 }
);
}
this is a code i copied and pasted from this site
it gives me the "navigator.geolocation"
but when it comes to "getCurrentPosition" my code stops working. Mobile Chrome works fine but this is not. I shared my position but still nothing happens. Any help will be appriciated.
Thanks.
Thanks everyone i found the solution,
i was getting the geolocation after some javascript operations. I tried to get the geolocation before document is ready. And it worked.
I know this is a bit old but it keeps coming up on searches so I thought I would add a tip that helped me.
Because I want to get the location right as the page loads I found that I needed to introduce a very short delay after the page loads. When I had no delay, I would get no error but also I would not activate the location protocols on the phone. This half second delay solved the issue. You can play with the delay and see if it solves your issues.
setTimeout(function() {getAutoLocation(true)},500);
I get the location in my "getAutoLocation(true)" function. This setTimeout only exists to introduce the delay.
seems like PhoneGap has a problem with geolocation
I have the same issue
I'm using S3 with Android 4.1.2, phonegap geolocation feature doesn't work
In order to get the geolocation without errors, you have to make that code block work before using the values provided by the geolocation because operations are carried out asynchronously, in this question i found the solution by loading my geolocation script before other .js files. This solved my problem and another trick for this issue is, geolocation works more stable when you give "always" permission for browser to read your location. After loading for the first time, you never encounter geolocation errors.
I found that some Android phones (old and new) don't run properly the function
getCurrentPosition, maybe trying to save some battery.
I played with the function watchPosition and then the high accuracy GPS kicked in.
Read this to know how to use the parameters properly:
http://dev.w3.org/geo/api/spec-source.html#watch-position
In my case, this worked:
{
maximumAge: 0, timeout: 2000, enableHighAccuracy: true
}
Hope this helps someone.
Did you give Internet permission in manifest?
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
I have created one app which locates the GeoLocation coordinates on iPad/iPhone but fails to do so on some of Android device even when the device is GPS enabled.It works on some of the Android Device(e.g Samsung Galaxy tab SCH-I800) and fails on some other(e.g Samsung Galaxy tab GT-P5113).
Any idea what could be the possible reason for the issue?
Sample code snippet:
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=true"> </script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script>
function onDeviceReady()
{
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
var onSuccess = function(position) {
pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
lat=position.coords.latitude;
lng=position.coords.longitude;
alert("lat: "+lat+" long "+lng); //i should get the lat/long here which i need to use further in my code
}
for devices where the app is not working it's going to onError function.
function onError(error) {
alert("error code is: "+error.code);//it gives error code 3 when on such devices
}
You need to set the "enableHighAccuracy" option when setting up the watch to use GPS on those Android devices:
navigator.geolocation.watchPosition(onSuccess, onError, { enableHighAccuracy: true });
See this answer also: Phonegap Android and GPS satellite
Your app doesn't work because of this error:
PositionError.TIMEOUT
Returned when the device was unable to retrieve a position within the time specified in the geolocationOptions' timeout property. When using in conjunction with geolocation.watchPosition, this error could be called into the geolocationError callback every timeout milliseconds.
Set the "enableHighAccuracy" option to false and then if you get code 2 error ( u might get sometimes ) , just
Go to
Menu--->Settings---->Location & Security
and then check
Use Wireless Network.
This has solved my problem and hope it solves yours as well...!!
I had this problem with one if my projects and my customer was adamant that he could go into maps and it would find Hus geolocation almost immediately whereas in my app it took forever or didn't work.
The solution was to go back in time on the plugins git repository and find the java files that were removed, and use that version.
What has happened is the plugin maintainers have just used the native browser geolocation , essentially not using a plugin at all, but in some phones it's just not up to scratch. An alternative could ve to use crosswalk in your app.
I know its not a "good" solution, but its what was necessary to get an acceptable performance.
I've tried the following in a number of 2.x android devices with phonegap version 2.0.0:
var onSuccess = function(position){
console.log("Success");
}
var onError = function(error){
console.log("Error");
}
gpsTrackWatchID = navigator.geolocation.watchPosition(
onSuccess,
onError,
{
enableHighAccuracy: true,
maximumAge: 5000,
timeout: 10000,
}
);
If I use the device indoors where there is no GPS signal onError is never called. The documentation states:
If the geolocationSuccess callback is not invoked within this time,
the geolocationError callback will be invoked with a
PositionError.TIMEOUT error code.
I've tried debugging this by putting a console.log in the watchPosition function in cordova.js, but the function seems never to be getting called. I wonder if the browser's native function is being executed instead of phonegap's?
First thing First, Do You set persmission in AndroidManifest correctly? Could You post the file here - especially the persission section?
Secondly, do You use Android 2.3.3 - if Yes, then You must use SDK with Google Maps APi as in normal API GPS location provider don't work.
Ant the last, do You have any JS errors on console, when registering for GPS location?