2016-03-28 81 views
0

我的JS腳本有點問題。 我想確定用戶在我的頁面上的位置和currant時區。 我使用Firefox,MS Internet Explorer和MS Edge測試了它。問題是,它有時有效,有時不起作用。不知道爲什麼。與JS的地理位置

在Firefox中我得到了請求,如果我真的允許這個函數。但我沒有得到經度和latidute。

如果我嘗試一下我的iPhone它一直工作。

function getLocation() { 
     navigator.geolocation.getCurrentPosition(showPosition); 
     function showPosition(position) {; 
      location_latitude = position.coords.latitude 
      location_latitude = location_latitude.toFixed(6); 
      location_longitude = position.coords.longitude 
      location_longitude = location_longitude.toFixed(6) 
      location_accuracy = position.coords.accuracy 
      location_accuracy = location_accuracy.toFixed(2); 

      var gmt = new Date(); 
      var location_gmt_difference = (gmt.getTimezoneOffset()/60/-1).toFixed(1); 

      options_location_input.location_time_zone.value = location_gmt_difference 
      options_location_input.location_latitude.value = location_latitude 
      options_location_input.location_longitude.value = location_longitude 
      options_location_input.location_accuracy.value = location_accuracy 
     }; 
    }; 

有人可以幫助我。我失散了!

回答

0

你是否總是正在服務該頁面?如果只是查看文件系統上的頁面,它不起作用;該頁面必須提供。

+0

嘿,是的。我在我的電腦上使用XAMPP進行服務。 – Kai

+0

如果您在本地運行,您可能會遇到的問題是您的xampp不知道在哪個公共IP地址正在運行,因此無法獲取地理定位。無論如何,客戶端的地理位置必須由用戶(合法)允許,否則您可以在服務器端代碼中獲取客戶端的請求IP地址。 – Kruser

+0

添加一個錯誤處理函數作爲getCurrentPosition的第二個參數來顯示爲什麼它不起作用。 https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition – arkadianriver