2016-05-17 32 views
-1

我正在嘗試將用戶的地理位置,然後做一個查詢。 在Mozilla Firefox中,它在Safari中也能正常工作......但在Chrome中它根本不起作用。Javascript地理位置不能在Chrome上工作

window.onload = function(){ 
    if(navigator.geolocation){ 

    navigator.geolocation.getCurrentPosition(function(position){ 

     var latitude = position.coords.latitude, 
      longitude = position.coords.longitude; 
     console.log(latitude + " " + longitude); 

    },handleError); 

    function handleError(error){ 
     //Handle Errors 
    switch(error.code) { 
     case error.PERMISSION_DENIED: 
      console.log("User denied the request for Geolocation."); 
      break; 
     case error.POSITION_UNAVAILABLE: 
      console.log("Location information is unavailable."); 
      break; 
     case error.TIMEOUT: 
      console.log("The request to get user location timed out."); 
      break; 
     case error.UNKNOWN_ERROR: 
      console.log("An unknown error occurred."); 
      break; 
    } 
    } 
    }else{ 
    // container.innerHTML = "Geolocation is not Supported for this browser/OS."; 
    alert("Geolocation is not Supported for this browser/OS"); 
    } 
}; 

,我也得到了error 1

User denied the request for Geolocation. 

但我沒有拒絕任何要求,實際上是彈出窗口犯規上來的。

我已進入Chrome設置>顯示高級>隱私>內容設置>位置允許所有設置。

重新啓動的Chrome並沒有發生任何事情。我確定我的代碼是100%合法的,所以有人知道如何處理它? 謝謝!

回答

2

最簡單的方法是點擊地址欄左側的區域並在那裏更改位置設置。它允許在網上,而不是設置甚至file:///位置選項和所有其他類型的

enter image description here