title: script doesn't allow geolocation.getCurrentPosition to finish its callback function
body: <p>I need to package the user's current location into an object and send it off to a PHP script.  So what I have currently is the following.</p>
  
  <p><strong>position object</strong></p>
  
  <pre><code>function position(lat, lon) {
      this.latitude = lat;
      this.longitude = lon;
  
      this.setlat = function(lat) {
          this.latitude = lat;
      };
  
      this.setlon = function(lon) {
          this.longitude = lon;
      };
  
      this.print = function() {
          alert(this.longitude + ", " + this.latitude);
      };
  }
  </code></pre>
  
  <p><strong>control flow</strong></p>
  
  <pre><code>var origin = new position(0, 0);
  
  $(document).ready(function() {
  
      getLocation();
      origin.print();
  
  
  });
  </code></pre>
  
  <p><strong>functions</strong></p>
  
  <pre><code>function getLocation() {
  
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(setLocation, showError, {timeout:60000});
      } else {
          alert("GeoLocation is not supported on this browser or device.");
          return null;
      }
  
  }
  
  function setLocation(position) {
      alert('callback success!');
      origin.setlat(position.coords.latitude);
      origin.setlon(position.coords.longitude);
  }
  
  function showError(error) {
      switch(error.code) {
          case error.PERMISSION_DENIED:
              console.log("User denied the request for Geolocation.");
              break;
          case error.POSITION_UNAVAILABLE:
               console.log("Location information is unavailable.");
              break;
          case error.TIMEOUT:
               console.log("The request to get user location timed out.");
              break;
          case error.UNKNOWN_ERROR:
               console.log("An unknown error occurred.");
              break;
      }
  }
  </code></pre>
  
  <p>The problem is that before the <code>setLocation</code> callback is being called, the main control flow continues and ignores the fact that I asked it get something for me.  I call getLocation and I get an alert of "0, 0" since that's what origin is originally set to.  So for some reason my <code>setLocation</code> callback just isn't getting called until everything else in the program is done.</p>
  
  <p>I've looked around in the documentation, but it's rather straightforward and there isn't a whole lot going on so it's hard to pin down why this is happening. </p>
  
  <p>I know a lot of people have struggled with this and there seems to be a lot of sort of 'look at this topic' but all of these topics point to code that is effectively the same as my own, but formatted a little differently.</p>
  
  <p>It's important to note that I really need this position data encapsulated in a global variable since I'll be comparing my location with several dozen other locations on page-load.</p>
  
  <p><strong>attempted solutions</strong><br>
  I've tried restricting my <code>navigator.geolocation.getCurrentPosition()</code> to a while loop that exits based on the completion of the callback function, but this always results in an unending loop.</p>
  
  <p>-I've tried playing with the <code>maximumAge</code> and <code>timeout</code> values in the <code>PositionOptions</code> in order to allow the function some time to finish, but again this just results in <code>setLocation</code> finishing after all other scheduled function calls are done.</p>
  
  <p>-I've tried using two seperate <code>$(document).ready(function(){});</code> in order to segment the <code>getLocation()</code> and <code>origin.print()</code> functions. </p>
  
  <p>-I've tried using <code>watchPosition</code> instead of <code>getCurrentPosition.</code></p>
  
  <p>Ultimately I really just need some clarification on why the callback function in <code>getCurrentPosition</code> isn't finishing until everything else does.</p>