2016-11-02 28 views
-1

我想從導航器中獲取緯度和經度,然後顯示它們。我的代碼不工作,它不顯示警報:使用javascript進行地理定位

$(document).ready(function() { 

    var long; 
    var lat;  

    if(navigator.geolocation) { 

     navigator.geolocation.getCurrentPosition(function(position) { 

      lat = position.coords.latitude; 
      long = position.coords.longitude; 

      alert(long + " " + lat); 
     }); 
    } 
+0

什麼不起作用? – RamenChef

+0

它沒有顯示警報。 – Juan

回答

0

我認爲你的代碼是好的。這個問題很可能是您嘗試在不安全的連接上使用Geolocation API。確保您在https上查看您的網頁。

+0

請參閱:https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation#Browser_compatibility – Splurtcake

+0

嗨!該頁面在我的電腦中。 – Juan

+0

您需要使用SSL證書(https)查看Web服務器上的頁面。該代碼不會在沒有SSL的情況下直接在您的電腦上運行。閱讀我上面發佈的文檔鏈接。 – Splurtcake

相關問題