Question

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.

Était-ce utile?

La solution 3

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.

Autres conseils

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

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>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top