Search code examples
androidbrowsergeolocationnavigator

navigator.geolocation.getCurrentPosition fails on Android Browser


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.


Solution

  • 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.