2017-04-12 57 views
0

我正在測試這個簡單的地理位置代碼,由於某種原因它不適用於iOS設備。我已經在MacOS,Windows,Windows Phone,Android手機等上進行過測試,它的工作原理與所有應用程序一樣。爲什麼這個HTML5地理位置代碼只能在iOS上獲取用戶的位置?

在授予權限後,我總是收到「POSITION_UNAVAILABLE」錯誤。

navigator.geolocation.getCurrentPosition(
    function success(position) { 
    var primary = getUniqueId(); 

    var url = endpointBaseurl + 
     '?property_code=' + propertyCode + 
     '&distribService=' + distribService + 
     '&lat=' + position.coords.latitude + 
     '&lon=' + position.coords.longitude + 
     '&distribTarget=' + primary; 

    //Update the line below to be one of your links. 
    addMenuItem('Offers', url); 
    }, 
    function error(err) { 
    switch (err.code) { 
     case 1: 
     alert("You must allow location in order to receive tobacco offers. Please refresh and try again.") 
     break; 
     case 2: 
     alert('POSITION_UNAVAILABLE'); 
     break; 
     case 3: 
     alert('Error 3'); 
     break; 
     default: 
     alert("Unable to determine your location. Please refresh and try again."); 
    } 
    }, options); 

我已經在物理設備上以及通過Browserstack進行了測試。該網站使用HTTPS運行。

任何想法爲什麼應該不工作,或者我應該如何去調試?我把頭髮拉過來。

+0

你在StackOverflow上看過[this](http://stackoverflow.com/questions/30998246/html5-geolocation-not-working-all-the-time-on-ios)問題嗎?這可能有一些幫助。 –

回答

0

編輯:this頁,它說以下內容:

地理位置的收購失敗,因爲位置的至少一個內源產生一個內部錯誤。

嘗試使用PositionError.message(在你的情況,err.message):

返回一個人類可讀的DOMString描述 錯誤的詳細信息。規格注意,這主要用於 調試使用,並不直接顯示在用戶界面中。


請確保您有開啓定位服務,並設置爲始終或同時使用在iOS的設置。

+0

謝謝,這裏是錯誤的對象; 'PositionError {code:3,message:「Timeout expired」,PERMISSION_DENIED:1,POSITION_UNAVAILABLE:2,TIMEOUT:3}' –

+0

我剛剛將超時時間增加到了20000, 'PositionError {code:2,message:「操作無法完成(kCLErrorDomain錯誤0.)」,PERMISSION_DENIED:1,POSITION_UNAVAILABLE:2,TIMEOUT:3}' –