navigator.geolocation.getCurrentPosition fails on Android Browser - android

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>

Related

Cordova geolocation accuracy gets capped at 10 meters

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!

Increasing timeout to get rid of "The server is taking longer than expected to respond" in a Cordova/PhoneGap app

I am using Cordova/PhoneGap for an iOS and Android app. All the app does is call "window.location.replace" in the onDeviceReady () function to redirect the browser to an external site. From then on, the "app" simply uses Cordova/PhoneGap's inbuilt browser for everything (not InAppBrowser).
One thing I have found is on occasion I receive this popup when the server may be running a little slower than usual:
The popup appears way too early, usually after just a few seconds. As I said, this happens on both iOS and Android.
Is it possible to increase the timeout in the Cordova/PhoneGap browser to something higher (eg. 60 seconds)? If that's not possible, is it possible to prevent this popup from showing up at all?
Thanks.
I managed to solve this myself.
The "Connection Error" popup that appears is not part of the core Cordova/PhoneGap code, but it is actually part of the cordova-plugin-remote-injection. It has a setting that defaults to 10 seconds before displaying that popup:
CRIPageLoadPromptInterval
<preference name="CRIPageLoadPromptInterval" value="5" />
Type: int
Default: 10
If the site hasn't loaded after this interval the user will
be provided a choice to continue waiting or to retry loading the site.
This is turned on by default. If not wanting the prompt the user set
the value to 0.
Setting this value in your config.xml file to a higher number (such as 60 if you want a 1 minute timeout) solves the problem. I have confirmed this by setting the value to a 1 second timeout and seeing the popup appear constantly.
I hope this helps anyone else that may have the same problem in future.

Meteor's cordova: geolocation plugin doesn't work on mobile

I'm deploying my Meteor (1.2.0.2) app to an Android device.
I installed the plugin by
meteor add cordova:cordova-plugin-geolocation#1.0.0
Added Cordova plugin cordova-plugin-geolocation#1.0.0.
I used the plugin by:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
navigator.geolocation.getCurrentPosition(function(p){ // onSuccess
console.log(p);
}, function(e){
console.log(e);
});
}
On desktop works perfectly (print p as Geolocation object) but on mobile it just ignores the callbacks (both success and errors).
I tested it in Chrome DevTools (chrome://inspect) by setting breakpoints. It steps until .getCurrentPosition call but then (either step over or step in) just skips both callbacks (and If I set a breakpoint within them, they are just not hit).
I even tried
navigator.geolocation.getCurrentPosition(function(p){ console.log(p);
both on Desktop console (works)
undefined
Geolocation { ... }
and mobile (does not work)
undefined
// nothing else is printed
Maybe has it something to do with app's permissions?
EDIT: I verified AndroidManifest.xml and the permissions are actually set by the plugin:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
#dragonmnl,
GPS is unreliable.
You did not talk about timeout, I must assume you have NOT set the timeout long enough. Try setting it to about 20 seconds -- as a minimum.
If your readings are poor, or you are timing out
go outside
make sure you have a clear view of all horizons (nothing blocking the view)
walk in a big circle, about 15ft in diameter
In about 30-45 seconds you should have a reading.
This will make sense when you read the link I gave you.
Jesse

Chromecast Receiver URL on Wordpress Site?

I'm trying to get the Sample Media Player App working, and have run into a problem. I have whitelisted my two URLs, and have gotten the app to run on my Android device without any issues.
My problem starts when actually attempting to cast the sample media. My Chromecast correctly displays as an option, however choosing it from the list does not yield any results on the Chromecast, itself (although it appears that my Android device connects without any issues). Could this be caused by my whitelisted URL not ending in ".html"? Here (link removed) is my page, for what it's worth.
Any help would be greatly appreciated.
Please check if the answer in this question (Chrome-cast Sample App doesn't work) can solve your problem.
Basically you need to set AppID in your receiver.html as well.
I think the problem is in MediaRouteHelper.registerMinimalMediaRouteProvider(mCastContext, this);
try to cheak its value, wheter it is returning true or false
if its returning false then it will not run properly

Why my phone-gap app has not been able to find geolocation on some of the Android device

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.

Categories

Resources