2012-01-08 46 views
0
navigator.geolocation.getCurrentPosition(
     function(pos) { 
     var fullpos = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude); 
     computePartners(fullpos); 
     } 
    ); 

是我所擁有的。它執行computePartners(),如果客戶端允許訪問他知道他在哪裏(獲取客戶的物理位置)。但是如果沒有,我想讓輸出成爲警報('錯誤'),例如。我怎樣才能做到這一點?谷歌地理api:如何處理客戶端拒絕許可

回答

3

我使用此代碼進行地理定位:

if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     // ok   
    }, function(error) { 
     if(error.code == 0){ 
     // unknown error 
     } else if(error.code == 1) { 
     // permission denied 
     } else if(error.code == 2) { 
     // position unavailable 
     } else if(error.code == 3) { 
     // timeout 
     } 
     console.log(error.message); 
    }, { 
     enableHighAccuracy: true, 
     maximumAge: 30000, 
     timeout: 10000  
    }); 
} else { 
     // browser do not support geolocation 
} 
+0

好的用戶接受和brwoser部分工作正常。但不是用戶拒絕,我試圖在那裏發出警報()。然後我訪問我的網站,當它問我說按「不是現在」,什麼也沒有發生.. – Karem 2012-01-08 23:50:38

+0

這很奇怪 - [文檔](http://developer.mozilla.org/En/Using_geolocation)表示,因爲我有書面。也許你必須像[某人建議]設置超時參數(http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt)?哪個瀏覽器使用使用? – hamczu 2012-01-08 23:55:44

+0

嘗試在鉻..現在工作!謝謝 – Karem 2012-01-09 00:01:31

相關問題