web-dev-qa-db-fra.com

Géolocalisation sans connexion SSL

Je développe une application qui utilise les coordonnées de géolocalisation . J'ai utilisé la fonctionnalité de géolocalisation HTML5 pour connaître l'emplacement du visiteur du site, mais le problème concerne Chrome, qui ne prend pas en charge la géolocalisation des origines non sécurisées.

  function showPosition(){
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(function(position){
            var positionInfo = "Your current position is (" + "Latitude: " + position.coords.latitude + ", " + "Longitude: " + position.coords.longitude + ")";
            document.getElementById("result").innerHTML = positionInfo;
        });
    } else{
        alert("Sorry, your browser does not support HTML5 geolocation.");
    }
}

Voici le code getCurrentPosition ne fonctionne pas car mon site n’est pas connecté SSL.

Avertissement par Chrome: getCurrentPosition () et watchPosition () ne fonctionnent plus sur des origines non sécurisées. Pour utiliser cette fonctionnalité, vous devez envisager de basculer votre application vers une origine sécurisée, telle que HTTPS.

Existe-t-il un autre moyen d'obtenir des coordonnées/des valeurs LatLong sur chrome? [sur connexion non sécurisée uniquement]

EDIT: Il fonctionne sur ma machine en utilisant localhost: 80 mais ne fonctionne pas dans l'URL de test qui est sur http

11
Niranth Reddy
var apiGeolocationSuccess = function(position) {
    alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};

var tryAPIGeolocation = function() {
    jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) {
        apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
  })
  .fail(function(err) {
    alert("API Geolocation error! \n\n"+err);
  });
};

var browserGeolocationSuccess = function(position) {
    alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};

var browserGeolocationFail = function(error) {
  switch (error.code) {
    case error.TIMEOUT:
      alert("Browser geolocation error !\n\nTimeout.");
      break;
    case error.PERMISSION_DENIED:
      if(error.message.indexOf("Only secure origins are allowed") == 0) {
        tryAPIGeolocation();
      }
      break;
    case error.POSITION_UNAVAILABLE:
      alert("Browser geolocation error !\n\nPosition unavailable.");
      break;
  }
};

var tryGeolocation = function() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(
        browserGeolocationSuccess,
      browserGeolocationFail,
      {maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
  }
};

tryGeolocation();
18
Mannan Bahelim

Voici ma solution non SSL pour Safari en 2017/05.

var apiGeolocationSuccess = function(position) {
    alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};

var tryAPIGeolocation = function() {
    jQuery.post( "https://www.googleapis.com/geolocation/v1/geolocate?key=AIzaSyDCa1LUe1vOczX1hO_iGYgyo8p_jYuGOPU", function(success) {
        apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
    })
        .fail(function(err) {
            alert("API Geolocation error! \n\n"+err);
        });
};

var browserGeolocationSuccess = function(position) {
    alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};

var browserGeolocationFail = function(error) {
    switch (error.code) {
        case error.TIMEOUT:
            alert("Browser geolocation error !\n\nTimeout.");
            break;
        case error.PERMISSION_DENIED:
            if(error.message.indexOf("Only secure origins are allowed") == 0) {
                tryAPIGeolocation();
            }
            break;
        case error.POSITION_UNAVAILABLE:
            // dirty hack for safari
            if(error.message.indexOf("Origin does not have permission to use Geolocation service") == 0) {
                tryAPIGeolocation();
            } else {
                alert("Browser geolocation error !\n\nPosition unavailable.");
            }
            break;
    }
};

var tryGeolocation = function() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            browserGeolocationSuccess,
            browserGeolocationFail,
            {maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
    }
};

tryGeolocation();

La nouvelle partie est celle-ci dans "case error.POSITION_UNAVAILABLE":

case error.POSITION_UNAVAILABLE:
// dirty hack for safari
if(error.message.indexOf("Origin does not have permission to use Geolocation service") == 0) {
    tryAPIGeolocation();
} else {
    alert("Browser geolocation error !\n\nPosition unavailable.");
}
break;
4
Eckonator

Chrome empêche désormais les sites non fiables d'utiliser la géolocalisation html5. Les sites de confiance incluent localhost ou n’importe quel domaine certifié https. Dans mon cas, ni l'un ni l'autre n'étaient possibles, j'ai donc ouvert chrome à partir de la ligne de commande avec cet argument: open -a /Applications/Google\ Chrome.app' --args --unsafely-treat-insecure-Origin-as-secure="http://yoursite.test" qui a ajouté mon domaine particulier en tant que site de confiance.

2
stackPusher

Pour moi, cela fonctionnait sous Mac OSX avec Chrome Version 70.0.3538.67 (Official Build) (64-bit)

 open -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir=$HOME --unsafely-treat-insecure-Origin-as-secure=http://www.bpl.test  --allow-running-insecure-content --reduce-security-for-testing
0
user969068