2012-02-09 64 views
14

好吧,我正在構建一個使用Geolocation API的應用程序。我似乎無法得到一個非常簡單的代碼到在Firefox 10在這裏工作是代碼:navigator.geolocation.getCurrentPosition回調不會在Firefox 10上運行

window.onload = function() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
       alert('it works'); 
      }, function(error) { 
       alert('Error occurred. Error code: ' + error.code);   
      }); 
     }else{ 
      alert('no geolocation support'); 
     } 
    }; 

因此,例如,在鉻,運行頁面後,我會問,如果我想分享我的位置,然後點擊是它會提醒我「它有效」。現在在Firefox 10中,它會要求我分享我的位置,點擊分享後它什麼都不做......我一直試圖讓回調運行任何類型的代碼,但沒有運氣。這是Firefox的錯誤還是我做錯了什麼?我有一個用於測試的代碼示例:http://dev-hub.com/geolocation.html

編輯--- 我的操作系統是Windows 7的64位

+0

你在運行firefox的硬件是什麼? – 2012-02-09 17:31:17

+0

在Windows 7上64 – Zaptree 2012-02-09 17:45:50

+0

地理定位功能需要支持地理定位的移動設備。 [你可以在這裏找到更多。](http://stackoverflow.com/questions/1349064/which-devices-support-javascript-geolocation-via-navigator-geolocation) – 2012-02-09 17:53:20

回答

18

好吧,我發現這個問題確實是Firefox和它不會在所有平臺上可靠的或同樣的工作。看着http://dev.w3.org/geo/api/spec-source.html我發現下面的選項中增加:

window.onload = function() { 
     if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
       alert('it works'); 
      }, function(error) { 
       alert('Error occurred. Error code: ' + error.code);   
      },{timeout:5000}); 
     }else{ 
      alert('no geolocation support'); 
     } 
    }; 

正如你可以看到這裏的超時時間:5000已添加這意味着如果由於某種原因,瀏覽器需要更多的則5000毫秒(5秒),然後拋出一個超時錯誤(這是錯誤代碼3)。所以,現在每當Firefox不工作時,它至少會運行錯誤回調,並收到「發生錯誤,錯誤代碼:3」的警報消息。

顯然超時的默認值是無限的,所以它永遠不會超時...... Chrome是100%可靠的,但Firefox在我的機器上可靠性約爲10%,這是非常令人失望的。在我的另一臺運行Windows XP並且位於同一網絡上的計算機上,Firefox似乎是100%可靠的。

+7

50000毫秒= 50秒...所以也許應該使用5000毫秒(3個零) – 2013-06-03 09:01:26

0

我做了這個例子給你:

if(!navigator.geolocation){ 
alert('El Navegador no soporta GeoLocalización'); 
} 

function doGeo(position) 
{ 
    var coords = position.coords.latitude + '+' + position.coords.longitude; 
    var url = 'https://maps.google.es/?q=' + coords; 
    $("#lat").html("Latitud: " + position.coords.latitude); 
    $("#lon").html("Longitud: " + position.coords.longitude); 
    $("#acc").html("Precisión: " + position.coords.accuracy); 
    $("#alt").html("Altitud: " + position.coords.speed);   
    var link = '<a class="btn btn-primary" href="' + url + '" target="_blank">Ir a la  Ubicación en Google Maps</a>'; 
    $(link).appendTo('#GoogleMaps'); 
} 

function lost() 
{ 
    alert('Algo salió mal, Intentelo más tarde...'); 
}; 
navigator.geolocation.watchPosition(doGeo, lost, {maximumAge:0,enableHighAccuracy:true}   ); 

http://jsfiddle.net/aA2zv/35/

希望它能幫助!

+0

Altitud ==速度? – 2016-02-26 16:29:06

+0

功能丟失了:( – 2016-07-20 19:44:40

相關問題