2017-08-26 70 views
-1

我有谷歌地理位置API問題。它正在返回我目前位置的經度緯度&。當我第一次集成時,它返回正確,但現在返回錯誤。請看看我的下面的JavaScript代碼。谷歌地理定位API問題

$(document).ready(function(){ 

      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=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 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."); 
         if (error.TIMEOUT) 
         { 
          tryAPIGeolocation(); 
         } 

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

      tryAPIGeolocation(); 

如果我運行上面的代碼我得到這個值(緯度:23.0401214)(經度:72.5163579),但這是錯誤的。它返回ahemdabad城市lat &長。

正確的值應該是:22.3039,70.8022

,請與我的問題,一起來看看。

回答

2

HTML 5瀏覽器提供的地理位置可以使用GPS設備(高精度)或蜂窩塔三角測量(中等準確度)來檢測客戶端的地理位置。

通過查看您的代碼,您的應用程序試圖儘早檢測用戶的地理位置,這很可能使用IP地址。

取決於您的用戶Internet服務提供商,IP地址到位置映射可能不準確。

可以爲用戶設備分配其地理位置註冊的IP地址與用戶的實際地理位置非常不同。

所以它是預期的行爲。在用戶同意同意分享他們的地理位置之前,這是您可以做的最好的努力。

+0

是的,但html 5地理定位服務要求https安全域不能用於http只。 –

+1

這是正確的。您需要使用https –

+0

好的,除了html5技術以外,沒有其他方法可以獲得用戶的確切位置嗎? –