Navigator.geolocation.getCurrentPosition / watchPosition does not work 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 tried both getCurrentPosition, and watchPosition.

It reaches a method errorCalback()when control comes to a string geolocation.watchPosition.

I am testing Motorola G 2nd Genwith Android 6and Google chrome browserand opera mini.

Update 1: When I put a warning in the callback function, I got a 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);
    })

2: g4s8 , URL. .. http https. , "". Do you want to allow location, . ?

+4
1

https API .

. API

Chrome 50, Chrome , API- HTML5 Geolocation ,

...

, , API https

, https localhost.


... ?

, . geoip2,


? ? , ?

GeoIP2 ip-. (geoip2.country()) (geoip2.city) js lib:

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

https://dev.maxmind.com/geoip/geoip2/javascript/ .

Google maps - google, api key. POST json https://www.googleapis.com/geolocation/v1/geolocate?key=API_KEY :

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

, , - , .

json " " https://developers.google.com/maps/documentation/geolocation/intro#overview

: getCurrentPosition() watchPosition()


IP, ..??

, .

, , getCurrent Position?

, , API- , .

. ( API ) https http .

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

Source: https://habr.com/ru/post/1658896/


All Articles