2014-04-25 45 views
1

我一直在尋找周圍的計算器今晚幾個小時尋找這個問題的解決方案,我強烈思考是一個cordova/phonegap與地理定位錯誤。爲什麼PhoneGap地理位置onSuccess不會第一次啓動?

我有相同的問題@Tal在這裏geoLocation enabling without page-refresh?

基本上從的onSuccess電話的getLocation永遠不會調用你第一次請求位置。我設置了一個超時來調用onError並休息頁面,但仍然無法弄清楚它爲什麼不能在第一時間工作,只有完全刷新才能解決問題。把它變成一個循環,即:

function onError(error) { 
    if (error.code === PositionError.TIMEOUT){ 
    navigator.geolocation.getCurrentPosition(onSuccess, onError, 
     { maximumAge: 3000, timeout: 1000, enableHighAccuracy: true }); 
    } 
    // Handle other errors here 
} 

似乎從來沒有工作。我試過重新安排我的代碼順序和功能,它似乎沒有任何區別。應用程序在第一次失敗後才能成功獲取GPS座標。我真的認爲它是一個科爾多瓦錯誤。我下面全javascript代碼:

<script type="text/javascript" charset="utf-8"> 
    // onSuccess Geolocation 
    // 
    function onSuccess(position) { 
     var element = document.getElementById('geolocation'); 
     //OUTPUT COORDINATES INFORMATION INTO HTML 
    element.innerHTML = '<h2>Detailed Gelocation information</h2><table class="geoTable"><tr><td class="noborder"></td><th>Latitude</th><th>Longitude</th><th>Altitude</th><th>Accuracy</th><th>Altitude Accuracy</th><th>Heading</th><th>Speed</th><th>Timestamp</th></tr>' + 
      '<tr><td class="head">Data Value</td>' + 
      '<td class="centre">' + position.coords.latitude + '</td>' + 
      '<td class="centre">' + position.coords.longitude + '</td>' + 
      '<td class="centre">' + position.coords.altitude + '</td>' + 
      '<td class="centre">' + position.coords.accuracy + '</td>' + 
      '<td class="centre">' + position.coords.altitudeAccuracy + '</td>' + 
      '<td class="centre">' + position.coords.heading + '</td>' + 
      '<td class="centre">' + position.coords.speed + '</td>' + 
      '<td class="centre">' + new Date(position.timestamp) + '</td></tr>'+ 
      '</table>' + 
      '<p align="center"><button data-theme="h" onclick="resetLocation();">Clear GPS Location</button></p>'; 


        //GET LOCATION VARIABLES FROM API 
        var lat = position.coords.latitude; 
        var lng = position.coords.longitude; 
        var yourStartLatLng = new google.maps.LatLng(lat, lng); 

        //PUT GMAPS INFORMATION INTO PAGE 
        $('#map_canvas').gmap({'center': yourStartLatLng}); 
        $('#map_canvas').gmap('option', 'zoom', 19); 
        $('#map_canvas').gmap('option', 'mapTypeId', google.maps.MapTypeId.SATELLITE); 
        $('#map_canvas').gmap('addMarker', { 
                 'position': yourStartLatLng, 
                 'draggable': false, 
                 'bounds': false 
                 }); 
    } 
    // onError Callback sends user an alert message and refreshes screen to load all scripts again 
    // 
    function onError(error) { 
     alert('The Freshwater application failed to get your location. Please ensure that you have a successful WIFI or 3G/4G internet connection when using the Geolocation feature. ' + 
       'The Locator screen will now be refreshed. Please attempt Geolocation again.'); 

    //navigator.geolocation.getCurrentPosition(onSuccess, onError, 
     //{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: false }); 
     //PUT A TIMER ON ERROR TO REFRESH PAGE FOR GPS 
     setTimeout("window.location+='?refreshed';", .1000); 
    } 

//RESET GEOLOCATION PAGE TO TAKE ANOTHER LOCATION READING 
    function resetLocation(){ 
     setTimeout("window.location+='?refreshed';", .1000); 
    } 

    //ATTACH FUNCTION TO BUTTON TO CALL GEOLOCATION 
    function getLocation() { 
     navigator.geolocation.getCurrentPosition(onSuccess ,onError, 
     { timeout: 1000, enableHighAccuracy: true }); 
    } 
</script> 
+0

您的科爾多瓦版本? – kathir

+0

對不起,忘了提。我正在使用最新版本,3.4 –

+0

我們已經在XDK(它提供了一個類似的webview環境)中看到一些類似的地理問題,並且認爲問題是由本地GPS實現引起的 - 結果往往會隨着Android版本和Android製造商的功能。希望我能提供一個解決方案... – xmnboy

回答

0

它的JavaScript加載時間,導致該地理位置功能超時的第一次。

我做了一些腳本修改,並擺脫了我沒有使用的許多Gmap UI Jquery庫。

jquery.ui.map.extensions.js 
jquery.ui.map.microdata.js 
jquery.ui.map.microformat.js 
jquery.ui.map.overlays.js 
jquery.ui.map.rdfa.js 
jquery.ui.map.services.js 

這可能有一個小的影響。當使用iOS模擬器進行基於時間的測試時,只要頁面加載不會在第一次啓動成功時詢問地理位置。如果我在點擊「獲取位置」按鈕之前等待大約10秒鐘,那麼它首次運行。

很明顯,這是在請求地理位置之前正確加載所有(內部和外部)腳本。我已經預裝外部谷歌地圖腳本像這樣:

function onLoad() { 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyDy1bBAiDhmFfuKz6SerLvKyOIUqlFsX7c&sensor=true&' + 
    'callback=mapLoaded'; 
    document.body.appendChild(script); 
    } 

但如果有人請求地理位置過快似乎超時而奔波onError的第一次。

更新:我已決定隱藏請求地理位置的按鈕,並在外部腳本已加載(並放置加載欄gif)時顯示它。當請求位置時它仍然會出錯。很可能是因爲谷歌API代碼本身包含一個getScript命令(所以我沒有看到真正的加載時間)。

Google可能會擺佈?

0

我想這可能是因爲你的設備不是'準備好'。請參見下面的代碼:

<script> 
    function onDeviceReady() { 
     // Do your geolocation call here ... 
    } 
    document.addEventListener("deviceready", onDeviceReady, false); 
</script> 

如果你把你的地理位置在通話設備準備情況,然後他們只會觸發一次設備被認爲是「準備好」。

相關問題