Prasanna Kumar Prasanna Kumar - 1 month ago 12
Android Question

navigator.geolocation.getCurrentPosition/watchPosition is not working in android 6.0

Here is my javascript code :

function getLocation() {
//navigator.geolocation.getCurrentPosition(getCoor, errorCoor, {maximumAge:60000, timeout:30000, enableHighAccuracy:true});
var mobile =jQuery.browser.mobile;
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if(mobile){
watchLocation(function(coords) {
var latlon = coords.latitude + ',' + coords.longitude;
//some stuff
}, function() {
alert("error");
});
} else {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
alert("error");
}
}
}

function watchLocation(successCallback, errorCallback) {
successCallback = successCallback || function(){};
errorCallback = errorCallback || function(){};
// Try HTML5-spec geolocation.
var geolocation = navigator.geolocation;
if (geolocation) {
// We have a real geolocation service.
try {
function handleSuccess(position) {
alert("position:"+position.coords);
successCallback(position.coords);
}
geolocation.watchPosition(handleSuccess, errorCallback, {
enableHighAccuracy: true,
maximumAge: 5000 // 5 sec.
});
} catch (err) {
errorCallback();
}
} else {
errorCallback();
}
}


I have tried both
getCurrentPosition
and
watchPosition
.

It's reaching
errorCalback()
method when control comes to
geolocation.watchPosition
line.

I am testing in
Motorola G 2nd Gen
with
Android 6
and
Google chrome browser
and
opera mini
.

Update 1: When I put alert in error call back function I got
error:1; message:Only Secure origins are allowed(see:
link).

navigator.geolocation.getCurrentPosition(showPosition, function(e)
{ alert(e); //alerts error:1; message:Only Secure origins are allowed(see: )
console.error(e);
})


Update 2: With the help from g4s8 I am able to findout that the error is because of insecure URL. i.e only accessing with
http
instead of
https
.But then also I bypassed that in browser by clicking advanced button.But it will prompt for
Do you want to allow location
, which I don't want..is there any way to access location without prompting it?

Answer

Your page should be served over https to access geolocation API.

See Geolocation API Removed from Unsecured Origins

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

...

It is an important issue as it will directly impact any site that requires use of the geolocation API and is not served over https

To fix this serve your page over https or on localhost.


Thank you...Is there any way to bypass it??

You can try to use some geolocation services, e.g. geoip2, Geolocation request


how to use them? can you show an example?? from those two can i access user location without knowing them?

GeoIP2 detect you location by ip address. You can obtain country (geoip2.country()) and city (geoip2.city) with js lib:

<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>

Here https://dev.maxmind.com/geoip/geoip2/javascript/ you can find full documentation.

Google maps geolocation is google service, so you need to get api key first. Then you can send POST request with json parameters to https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY and get the response:

{
  "location": {
    "lat": 51.0,
    "lng": -0.1
  },
  "accuracy": 1200.4
}

where location is the user’s estimated latitude and longitude, in degrees, and accuracy is the accuracy of the estimated location, in meters.

Full json parameters defenition you can find in "Request body" section here https://developers.google.com/maps/documentation/geolocation/intro#overview

Alos you can find usedul those answers: getCurrentPosition() and watchPosition() are deprecated on insecure origins


using IP it provides only country and city..??

Yes, only this.

will it provide physical location like how getCurrent Position provides??

No, you can't get physical location, because it can be accessed only via gelocation API, that was restricted in insecure context.

Also you have one more option. You can host only one page (that access geolocation API) on https server, and redirect from this page to your http site with user location in get parameters.

/* https page */
navigator.geolocation.getCurrentPosition(function (result) {
    window.location.href = "http://your.site.com/http-page?lat=" + result.latitude + "&long=" + result.longitude;
});
Comments