title: Wait for geocoder function to finish executing then proceed then continue
body: <p>I am working on a hybrid app using the Appgyver Steroids framework and I'm trying to implement a 'detect user location' feature whereby the user can toggle a switch to choose whether they'd like their location (long &amp; lat) to be detected automatically, or alternatively they can enter their location (city, postcode or county) into a text box and the longitude and latitude will be calculated on click of a button based on their input/selection. </p>
  
  <p>When the user toggles the switch into the 'on' position and taps submit, the <code>navigator.geolocation.getCurrentPosition</code> is fired and calls the relevant functions which then stores their current longitude and latitude in <code>localStorage</code>. This works perfectly.</p>
  
  <p>However, when the user toggles the switch into the 'off' position, my geocode function [manuallyGeoCode()] which codes their location into long and lat doesn't seem to fire in time and so the alert is fired straight after calling that geocode function before it has had time to actually set the <code>localStorage</code> value. I've researched using a callback and I've looked into using the jQuery <code>deferred</code> method, both of which I've had no success with using. Any help would be massively appreciated! Thanks for reading.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>    &lt;h3&gt;Your location&lt;/h3&gt;
        &lt;ul class="list"&gt;
          &lt;li class="item item-toggle"&gt;Use my current location
            &lt;label class="toggle toggle-balanced"&gt;
              &lt;input type="checkbox" id="myLocationToggle" checked="true"&gt;
              &lt;div class="track"&gt;
                &lt;div class="handle"&gt;&lt;/div&gt;
              &lt;/div&gt;
            &lt;/label&gt;
          &lt;/li&gt;
          &lt;li class="item item-input"&gt;
            &lt;input type="text" id="userLocation" placeholder="City, town or postcode" disabled="true"&gt;
          &lt;/li&gt;
        &lt;/ul&gt;
  
  &lt;button class="button button-balanced" id="getLongLat"&gt;Get long/lat&lt;/button&gt;
  
  
  $(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] &amp;&amp; place.address_components[0].short_name || ''),
                    (place.address_components[1] &amp;&amp; place.address_components[1].short_name || ''),
                    (place.address_components[2] &amp;&amp; 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);
  </code></pre>