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.
You must use HTTPS
Starting with Chrome 50, Chrome no longer supports obtaining the user’s location using the HTML5 Geolocation API from pages delivered by non-secure connections. This means that the page that’s making the Geolocation API call must be served from a secure context such as HTTPS.
https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only?hl=en
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
.
After searching around for a solution to the same problem, having come to the conclusion that the timeout needs to be set much higher for android devices, i implemented the following as a base:
function setGeoLocation(){
var ua = navigator.userAgent.toLowerCase(),
isAndroid = ua.indexOf("android") > -1,
geoTimeout = isAndroid ? '15000' : '1000';
function success (position) {
console.log(position);
};
function error (err) {
console.log('error message');
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy: true, maximumAge: 3000, timeout:geoTimeout});
} else {
error('Location services must be enabled to use this');
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With