0

我正在使用 Appgyver Steroids 框架开发一个混合应用程序,我正在尝试实现“检测用户位置”功能,用户可以切换开关以选择他们是否希望检测到他们的位置(经纬度)自动,或者他们可以在文本框中输入他们的位置(城市、邮政编码或县),然后根据他们的输入/选择单击按钮计算经度和纬度。

当用户将开关切换到“打开”位置并点击提交时,将navigator.geolocation.getCurrentPosition触发并调用相关函数,然后将其当前的经度和纬度存储在localStorage. 这完美地工作。

但是,当用户将开关切换到“关闭”位置时,我的地理编码功能 [manuallyGeoCode()] 将他们的位置编码为 long 和 lat 似乎没有及时触发,因此在调用该地理编码后立即触发警报函数在它有时间实际设置localStorage值之前。我使用回调进行了研究,并研究了使用 jQuerydeferred方法,这两种方法我都没有成功使用。任何帮助将不胜感激!谢谢阅读。

这是我的代码:

    <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);
4

1 回答 1

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.');
      }
    });
}
于 2014-09-03T19:54:57.213 回答