2016-05-08 9 views
3

Gibt es eine Fallback-Option für Geolocation API von Google?Geolocation-API aus ungesicherten Ursprüngen in Chrome entfernt 50

Ausgehend von Google Chrome-Version 50, alle Anfragen von ungesicherter Herkunft Geolocation API zurückgeben Fehlercode 1.

https://developers.google.com/web/updates/2016/04/geolocation-on-secure-contexts-only?hl=en

Als Ergebnis bin ich nicht in der Lage den aktuellen Standort der Nutzer zu lokalisieren aus Chrome-Browser.

Ich möchte meinen Server nicht von HTTP zu HTTPS verschieben.

+0

Versuchen [gpsha.re] (https://gpsha.re). Sie können Ihre Standortanforderungen auf diese Seite umleiten. – Dennisvdh

Antwort

0

Dieser Kerl machen einige Fallback, wenn es passiert. http://jsfiddle.net/gogs/jwt9f1o3/

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(); 
Verwandte Themen