Cordova - Geolocation seems very unstable on different devices - android

I have created a mobile app with cordova. This app uses the GPS location of the device. Now the issue is the cordova geolocation plugin seems not to be working on other devices especially on the nexus 7. See sample function below.
navigator.geolocation.getCurrentPosition(function (pos) {
//something here
}, location_error, {timeout: 60000, enableHighAccuracy: true, maximumAge: 3600000});
I tried a lot of solutions like setting timeout and enabling high accuracy. And the weirdest part is, when i restart the device geolocation works!
Does anyone have any solution for this, or any alternative?

Related

Meteor on android device can't get GPS position

I have a mobile application done in Meteor + ionic and I'm trying to get the user GPS location using this simple code:
navigator.geolocation.getCurrentPosition( function ( result ) {
console.log( result );
Session.set( 'pos', result );
}, function ( err ) {
console.log( err );
});
All I get is a warning saying that getCurrentPosition deprecated on insecure locations.
Why is my app seen as an insecure location since I'm running it with meteor run android-device which basically installs it on my phone then runs it like any other app?
If I open up the chrome inspector and control the app's browser I can switch the url from http://meteor.local to https://meteor.local making it a "secure location" but all I get is a timeout error when getCurrentPosition is fired.
So...how does one get the user's position inside a meteor mobile application?
According to the documentation you need to use the cordova geolocation plugin. I believe this is because the webview used by cordova may not have all the features of chrome on your mobile -- which is a natural but wrong assumption.
According to the geolocation plugin readme you need to add the plugin:
meteor add mdg:geolocation.
and then you can use:
Geolocation.currentLocation()
There might be a way to do this all with navigator.geolocation, but if you use the native functionality provided via the plugin then you won't need to worry much about security consideration in browsers. It will also ensure that things will work on older devices that may use a version of the webview that doesn't yet implement the geo API.
I had the same problem when using the navigator.geolocation. It seems like the error function will never be fired because the timeout default value is actually infinity. To solve it is to actually set a minimum timeout value.
navigator.geolocation.getCurrentPosition(success,error,{timeout:5000});

GetPositionAsync does not return on real device when indoors - Xamarin Android

I am developing both and iOS and Android version that requires GPS using Xamarin.
On iOS it's working fine, however now developing the Android version I have some issues with the GPS. It does not fetch the GPS but times out. However, on a emulator (using Genymotion) it fetches a coordinate right away. Note that I am using the Xamarin.Mobile (latest plugin 0.7.5).
private async Task<HelpPosition> CreateGeolocator ()
{
int accuracy = SettingsManager.Instance.GetSettings ().Accuracy;
Geolocator locator = new Geolocator (this) { DesiredAccuracy = accuracy * 10 };
Position p = await locator.GetPositionAsync (timeout: 10000);
return new HelpPosition{ Latitude = p.Latitude, Longitude = p.Longitude };
}
This always code times out after 10s. I have set the ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION permissions.
The issue seems only to occur when indoors. However Google Maps is perfectly fine getting my exact position. And I have checked all settings in Location Services (Use wireless networks and Use GPS satelites). The device is a Samsung Note 2.
Never got Xamarin.Mobile working to satisfaction on Android indoors so I tested the only other option in Component Store avaliable: Geolocator. Works like a charm!
https://components.xamarin.com/view/GeolocatorPlugin
Edit It works better, but even with this plugin I get reports that the location received is off. The search for a better solution continues...

Phonegap Geolocation on Android

I am using Phonegap Build version 3.4.0 with the geolocation plugin version 0.3.7.
My app should display the users location on a map.
This works very well under iOS.
But depending on the location settings in Android I am getting very different results.
If I am trying to get a quick location fix with enableHighAccuracy: false and the user has disabled Wireless Location Access, my error callback gets called with the error code 2. This is what I expected.
But if I am trying to get the best result with enableHighAccuracy: true and the user has disabled GPS Access the error callback does not get called. Instead it just times out.
Is this known behaviour and I have to live with it or is there something I could do?
I am using boilerplate code:
var positionOptions = {
enableHighAccuracy: true,
timeout: 1000*30,
maximumAge: 0
};
function positionSuccess(location) {
console.log(location);
};
function positionError(error) {
console.log(error);
};
navigator.geolocation.getCurrentPosition(
positionSuccess,
positionError,
positionOptions
);
Edit: this is tested on Huawei/Android 4.0.3, Moto G/Android 4.4.2, Samsung GT-I9100/Android 4.1.2, LG ???/Android 4.1.2
The geolocation on Cordova can be very tricky to get working correctly. I'm still occasionally battling with it myself. If you get a timeout make sure the geolocation code is even compatible with your test device by extending the timeout to ~300,000 ms (5 minutes). Yeah, if you're in a region with a bad signal it can take that long. Also try getting your location via wifi. And test on a live device (check out TestDevice). Android simulators are iffy.
If geolocation is crucial to your application, you should alert your users of the problem when you get each of these error codes. Tell your user that they need to enable one form of access or another and how to go about doing that.
Also, you can consider running getCurrentPosition where enableHighAccuracy: false, and if that fails run getCurrentPosition again but this time set enableHighAccuracy: true. Only after both methods have timed out, prompt the user with an error.
By the way, in my experience while on an Android, Cordova is usually able to lock onto a location within 2-3 seconds when using 3/4G in an urban environment. If not by then it pretty much never locks on. Good luck!

geolocation.watchposition not working with network based location off

I am building a location aware app for Android using sencha 2.3.1 + phonegap 3.3.0, and use geolocation to show a list of destinations ordered by proximity to the user location.
Everything works fine if the setting for network-based location is on. But if I turn it off and leave gps only based location then it never gets detected.
The code is very basic, I tried both the sencha path:
Ext.device.Geolocation.watchPosition({
frequency: 10000,
callback: function(position) {
AppHelper.setCurrentLocation(position);//yeah!!
},
failure: function() {
AppHelper.setLocationModeOff();//oouch!
}
});
and the phonegap path:
var success = function(position) {
AppHelper.setCurrentLocation(position);
};
var fail = function() {
AppHelper.setLocationModeOff();
};
navigator.geolocation.watchPosition(success, fail,{ timeout: 30000 });
Result is the same, I tried waiting for a couple minutes for the geolock to happen but no sign of location service.
The phonegap location plugin is installed, the permissions are correct, and everything works if network location is enabled.
I am testing on a Sony Tablet S.
I am aware of this bug:
How to get user position by GPS in Sencha
But I checked the code and its is definetely fixed in sencha 2.3.1
I wil try on other devices but I was wondering if anybody experienced the same.
TIA.
EDIT:
I made an experiment using standard html5 code on my android device using systems default browser (webkit based).
I would access my settings page and change settings at each run.
Accessing http://html5demos.com/geo I get the following:
I started the device with network geolocation on and gps based location off:
I get a message that asks if I want to share my location I say yes, only this time everything works fine.
Turn off network based geolocation, gps still off:
Location detection fails. (as expected)
Turn on GPS, leave network geolocation off:
Geolocation fails silently, I don't get a request to use my location and subsequent activation of GPS based gelocation.
So my conclusion is: this is similar to what I noticed in phonegap. If network based geolocation is on everything works as expected. But if I have no network connection and gps is on that is pretty useless in my html5 app.
At least this is what happens on my device (sony tablet)
From my understanding of HTML5 geolocation API, that is expected behaviour, you just can't control if under the hood it is using gps or network location.
What's probably happening is that watchPosition(), when it is called, tries first a raw, quick positioning using network, then goes with the gps positioning to give you a better result (assuming you called it with the enableHighAccuracy parameter).
If you are disabling network positioning it probably gets stuck at the first point: as stated in cordova/phonegap's docs the permission to access coarse location is a requirement for geolocation to work.
(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I am guessing that revoking access to coarse location from the device settings somehow "overrides" the user permission given when installing the app.

Android Device never receives location updates

I ran into a peculiar problem today: We need the users geolocation to provide our service and it is working well so far (implementation before the recent Android Location API release, have not updated it yet). We register like this
((LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE))
.requestLocationUpdates((provider == WPS)?
LocationManager.NETWORK_PROVIDER : LocationManager.GPS_PROVIDER,
minTime, minDist, onChange(ctx, provider)); // onChange create a pending intent
and on certain events we call
Location gpsLocation = LocMngr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location wpsLocation = LocMngr.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
On a HTC One S running on Android 4.0.3 and HTC Sense 4.0 we never receive location updates and the last known location returns null for gps and a 2 day old location for wps. WIFI, GPS and Mobile Data is turned on, i've tried walking around outside and saw the device trying to get a gps fix, but apparently it did not get one. Other devices have no problem with receiveing location updates from gps or wps using the same code...
This only occurs on this device (it's a test device so bootloader is locked, not rooted or any other funky stuff that might break brick it)
I am at a loss, any ideas?
UPDATE
Since we rebooted the device the problem did not occur again, I am still wondering what caused the problem...
UPDATE 2
I ran into the same Problem yesterday with a Galaxy Nexus official Android 4.2.2. So this seems to be a problem of Android in general.
Now that apparently nobody knows how to solve this, is there any way to detect when the Location API is not sending any fixes anymore? Apart from checking the timestamp of the lastKnownLocation()?

Categories

Resources