2016-09-07 29 views
8

Coğrafi konum koordinatlarını kullanan bir uygulama geliştiriyorum. Site ziyaretçisinin yerini bulmak için HTML5 coğrafi konum özelliğini kullandım, ancak sorun, güvenli olmayan kökenlerde Coğrafi Konum'u desteklemeyen Chrome'la ilgili.Coğrafi Konum SSL bağlantısı olmadan

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."); 
    } 
} 

İşte sitemde bağlı SSL olmadığı kadar getCurrentPosition çalışmıyor koddur.

Chrome ile Uyarı: getCurrentPosition() ve watchPosition() artık güvensiz kaynaklarda çalışmaz. Bu özelliği kullanmak için, uygulamanızı HTTPS gibi güvenli bir kaynağa geçirmeyi düşünmelisiniz.

Kromda koordinatlar/LatLong değerleri almanın başka bir yolu var mı? [Güvensiz bağlantıda sadece]

DÜZENLEME: Bu localhost kullanarak benim makinede çalışıyor: 80 ama http üzerindedir Test url çalışmıyor İşte

+0

Googled. Ama iyi bir çözüm bulamadı. –

+0

Açık soru: HTTPS kullanmaya başlamak bir seçenek değil…? – deceze

+0

Https bir seçenektir. Ancak sadece herhangi bir alternatif olup olmadığını öğrenmek istediniz –

cevap

13
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(); 
+1

Eh, sanırım bu çalışıyor. N'yi güncelleyeceğiz –

+1

Bu, API anahtarını değiştirdikten sonra çalıştı. –

+0

Bu çözüm, doğru koordinatlara dönmüyor –

3

Safari için benim SSL olmayan çözüm olduğunu

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; 
: 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(); 

yeni bölüm "vaka error.POSITION_UNAVAILABLE" bu biridir

İlgili konular