KickingLettuce KickingLettuce - 7 months ago 33
Javascript Question

JavaScript Geolocation failing in all Android browsers - working in iOS and PC

Here is my very basic code:

if (navigator.geolocation) {

// WILL GET TO THIS POINT WITH TEST `ALERT()`

navigator.geolocation.getCurrentPosition(

// WILL NOT GET OT THIS POINT IN ANDROID BROWSER

function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
}, showError, {
enableHighAccuracy: true,
timeout : 5000,
maximumAge: 0
}
);
} else {
return alert('No Geolocation Support.');
}
};


It works great in iOS (Safari and Chrome); and all PC browsers I've tried.

On Android, I've tried the stock HTC Browser, Chrome and Dolphin. The Satellites look like it is searching then, it stops. I don't even recall it asking for my permission to use geolocation (could have overlooked that part)

UPDATE: It does work on my Nexus 10 Chrome browser. But not my HTC One X.

UPDATE 2 - This appears to ONLY be happening on one Android device, an AT&T HTC One X. All other Android devices, PC browsers and iOS work fine. On the One X I get the Error Code: Timeout. Also, GPS works fine on this device otherwise.

Answer

Your code is absolutely correct. I have tested this in Android 2.3 + to latest version in different devices including HTC, Samsung, Asus tablets and Samsung tablets. Even in tablets, It is working fine. Check if the device has settings of share location false in device. When a page script is requesting for location browser will ask for user permission in android device browser. May be you have clicked never share your location. Please check it by clearing the browser settings in your device. I guarantee that there is no geolocation api problem in android 2.3+ without messing up with security settings.

And if still problem persists and you want add at least some polyfill then check this https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills and have some luck. They calculate location based on your IP usig geoIP database.

Comments