Navigator.Geolocation.Getcurrentposition Sometimes Works Sometimes Doesn'T

navigator.geolocation.getCurrentPosition not working

This is a known location bug in react native and specially Android. Do not send the third parameter (the options one) and see if that works.

If has to do with the highAccuracy and maximumAge

Problem with Navigator.geoLocation.getCurrentPosition not working

Your problem is that the Geolocation API is available only on HTTPS(secure contexts), and doesn't work on HTTP, hence it's denying the permission.
From docs,

This feature is available only in secure contexts (HTTPS), in some or
all supporting browsers.

Also see your Browser Compatibilty.

EDIT:

To enable HTTPS with Nginx, you have to generate SSL certificate and modify the configuration file. Follow this manual for detailed steps.

navigator.geolocation.getCurrentPosition fails on Android Browser

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.

navigator.geolocation.getCurrentPosition() doesn't work

I just tried your jsfiddle and it worked fine (Chrome 19 on Win 7), so I can't see why it should be causing problems.

Having said that, I'd strongly recommend switching to use navigator.geolocation.watchPosition instead of navigator.geolocation.getCurrentPosition. I recently did some work on a geolocation system and found getCurrentPosition can return unreliable cached positions, even if you use the options parameter to specify a low maximumAge value.

My latest version stops the watch after one of the following is true:

  • the fifth position has been returned
  • the accuracy is under 100m
  • the time since the watch began is over 30 seconds

navigator.geolocation.getCurrentPosition() never returns in WebView on Android

Try with options to set timeout (source):

var options = {
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
};

navigator.geolocation.getCurrentPosition(success, error, options);

If it fails then try to override getCurrentPosition (source):

(function() {

if (navigator.geolocation) {
function PositionError(code, message) {
this.code = code;
this.message = message;
}

PositionError.PERMISSION_DENIED = 1;
PositionError.POSITION_UNAVAILABLE = 2;
PositionError.TIMEOUT = 3;
PositionError.prototype = new Error();

navigator.geolocation._getCurrentPosition = navigator.geolocation.getCurrentPosition;

navigator.geolocation.getCurrentPosition = function(success, failure, options) {
var successHandler = function(position) {
if ((position.coords.latitude == 0 && position.coords.longitude == 0) ||
(position.coords.latitude == 37.38600158691406 && position.coords.longitude == -122.08200073242188))
return failureHandler(new PositionError(PositionError.POSITION_UNAVAILABLE, 'Position unavailable'));

failureHandler = function() {};
success(position);
}

var failureHandler = function(error) {
failureHandler = function() {};
failure(error);
}

navigator.geolocation._getCurrentPosition(successHandler, failureHandler, options);

window.setTimeout(function() { failureHandler(new PositionError(PositionError.TIMEOUT, 'Timed out')) }, 10000);
}
}
})();

As a third option annotate with @JavascriptInterface (source) in EmbeddedChromeClient

Also add at the proper place in your code:

mWebSettings.setJavaScriptEnabled(true);
//...
mWebSettings.setSupportZoom(true);
webView.addJavascriptInterface(new EmbeddedChromeClient(webView), "injectedObject");
webView.loadData("html here", "text/html", null);

The last option is just to use tags in html, load the html from disk storage, replace tags in the calling function, load the html/string in the webView. I have used this approach before in Android when positioning frustrated me too much. Then you don't have to worry about https either.

Calling navigator.geolocation.getCurrentPosition() freezes my NextJS app

I found what the problem was.

I was using navigator.geolocation.watchPosition in another component of my tree. The fact is that navigator.geolocation.getCurrentPosition doesn't work as expected when declaring watchPosition.

I should have used clearWatch.

Anyways, it's still a weird behaviour and I think I won't be the only one with this problem in the future, so I won't delete my post.

Thank you



Related Topics



Leave a reply



Submit