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;
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
watchLocation(function(coords) {
var latlon = coords.latitude + ',' + coords.longitude;
//some stuff
}, function() {
} else {
if (navigator.geolocation) {
} else {

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) {
geolocation.watchPosition(handleSuccess, errorCallback, {
enableHighAccuracy: true,
maximumAge: 5000 // 5 sec.
} catch (err) {
} else {

I have tried both

It's reaching
method when control comes to

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

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

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

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
instead of
.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 there any way to access location without prompting it?


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 ( and city ( with js lib:

<script src="//" type="text/javascript"></script>

Here 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 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

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 = "" + result.latitude + "&long=" + result.longitude;