2014-09-03 98 views
0

我正在使用Appgyver類固醇框架混合應用程序,我試圖實現'檢測用戶位置'功能,從而用戶可以切換開關以選擇是否他們希望自動檢測他們的位置(長& lat),或者他們可以將他們的位置(城市,郵政編碼或縣)輸入到文本框中,經度和緯度將通過點擊按鈕來計算輸入/選擇。等待地理編碼器功能完成執行,然後繼續然後繼續

當用戶切換到「開」位置並點擊提交時,navigator.geolocation.getCurrentPosition被觸發並調用相關函數,然後將其當前的經度和緯度存儲在localStorage中。這工作完美。但是,當用戶將開關切換到'off'位置時,我的地理編碼函數[manualGeoCode()]將它們的位置編碼爲long和lat似乎沒有及時觸發,所以直接觸發警報在它已經有實際設置localStorage值之前調用該地理編碼功能之後。我研究過使用回調函數,我研究過使用jQuery deferred方法,這兩種方法我都沒有成功使用過。任何幫助將大量讚賞!謝謝閱讀。

這裏是我的代碼:

<h3>Your location</h3> 
     <ul class="list"> 
     <li class="item item-toggle">Use my current location 
      <label class="toggle toggle-balanced"> 
      <input type="checkbox" id="myLocationToggle" checked="true"> 
      <div class="track"> 
       <div class="handle"></div> 
      </div> 
      </label> 
     </li> 
     <li class="item item-input"> 
      <input type="text" id="userLocation" placeholder="City, town or postcode" disabled="true"> 
     </li> 
     </ul> 

<button class="button button-balanced" id="getLongLat">Get long/lat</button> 


$(function(){ 
    AutoGeoCode(); 
}); 

function AutoGeoCode(){ 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(onSuccess, onError); 
    } 
} 

$('#getLongLat').on('click',function(){ 
    localStorage.latToPost = ''; 
    localStorage.lngToPost = ''; 

    if(localStorage.userLatAutoDetected != '0' || localStorage.userLngAutoDetected != '0'){ 
    localStorage.latToPost = localStorage.userLatAutoDetected; 
    localStorage.lngToPost = localStorage.userLngAutoDetected; 
    } 
    else{ 
    manuallyGeoCode(); // this doesn't finish in time so it jumps to the alert below and shows empty values. 
    } 

    alert('geodata is: {'+localStorage.latToPost+'}, {'+localStorage.lngToPost+'}'); 

}); 

$('#myLocationToggle').on('click',function(){ 
    if($(this).is(':checked')){ 
    $('#userLocation').val('').prop('disabled',true); 
    AutoGeoCode(); 
    } 
    else{ 
    $('#userLocation').val('').prop('disabled',false); 
    localStorage.userLatAutoDetected = '0'; 
    localStorage.userLngAutoDetected = '0'; 
    } 
}); 

function onSuccess(position){ 
    localStorage.userLatAutoDetected = position.coords.latitude; 
    localStorage.userLngAutoDetected = position.coords.longitude; 
} 

function onError(error){ 
    alert('current location could not be auto detected. Error: ' + error); 
} 

//Autocomplete location search box 
function initialize() { 
    var address = (document.getElementById('userLocation')); 
    var autocomplete = new google.maps.places.Autocomplete(address); 
     autocomplete.setTypes(['geocode']); 
    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
    var place = autocomplete.getPlace(); 
    if (!place.geometry) { 
     return; 
    } 
    var address = ''; 
    if (place.address_components) { 
     address = [ 
        (place.address_components[0] && place.address_components[0].short_name || ''), 
        (place.address_components[1] && place.address_components[1].short_name || ''), 
        (place.address_components[2] && place.address_components[2].short_name || '') 
       ].join(' '); 
    } 
    }); //end google.maps.event 
} 

function manuallyGeoCode(){ 
    var address = $('#userLocation').val(); 
    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     localStorage.latToPost = results[0].geometry.location.lat(); 
     localStorage.lngToPost = results[0].geometry.location.lng(); 
     } 
     else { 
     alert('Your location could not be geocoded.'); 
     } 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
+0

在geocoder.geocode的回調應有助於發送DIFF回調manuallyGeoCode並調用相同 – Sunand 2014-09-03 19:38:50

+0

@孫娜 - 我剛剛嘗試過,它仍然觸發警報併產生「地理數據是:{},{}」。是否因爲我在if {else ... else {}塊中調用'manualGeoCode()'? – user1163073 2014-09-03 19:49:05

回答

0

請找到手柄和差異手動地理編碼功能

$('#getLongLat').on('click',function(){ 
    localStorage.latToPost = ''; 
    localStorage.lngToPost = ''; 

    if(localStorage.userLatAutoDetected != '0' || localStorage.userLngAutoDetected != '0'){ 
    localStorage.latToPost = localStorage.userLatAutoDetected; 
    localStorage.lngToPost = localStorage.userLngAutoDetected; 
    alert('geodata is: {'+localStorage.latToPost+'}, {'+localStorage.lngToPost+'}'); 
    }else{ 
    manuallyGeoCode(function(){ 
     alert('geodata is: {'+localStorage.latToPost+'},{'+localStorage.lngToPost+'}'); 

    }); // this doesn't finish in time so it jumps to the alert below and shows empty values. 
    } 
}); 

function manuallyGeoCode(cb){ 
    var address = $('#userLocation').val(); 
    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     localStorage.latToPost = results[0].geometry.location.lat(); 
     localStorage.lngToPost = results[0].geometry.location.lng(); 
     cb(); 
     } 
     else { 
     alert('Your location could not be geocoded.'); 
     } 
    }); 
} 
+0

這提醒了正確的地理數據,但是當我在if {} .. else {}外部提示'localStorage.latToPost'或'localStorage.lngToPost'時,它仍然顯示警報但沒有地理數據。如果這有道理?原因是,我想通過ajax將localStorage數據發佈到我的web服務。 – user1163073 2014-09-03 20:05:24

+0

所以,當你使用警報,那裏使用自定義函數將這些值作爲參數,並在那裏發送請求 – Sunand 2014-09-03 20:07:53

+0

感謝您的幫助。問題是,當我在'if else'塊之後放置一個'$ .ajax'請求時,在'manualGeoCode'有機會回調地理數據並將這些值存儲在'localStorage'之前,'ajax'塊會被執行。 – user1163073 2014-09-03 20:53:30

相關問題