Call location coordinates from outside the function
<p>I'm try to access the lat and long from getCurrentPosition so that I can create a URL to use with an API. This is working if you call it from within the function or if you set a timeout on an alert(outside the function), as the current position takes a few moments to determine the coordinates. How can I access coords variables and/or the URL one? Thank you</p>

<pre><code>  var coords1;
  var coords2;

  if (Ti.Geolocation.locationServicesEnabled) {
  Titanium.Geolocation.purpose = 'Get Current Location';
  Titanium.Geolocation.getCurrentPosition(function(e) {
    if (e.error) {
        Ti.API.error('Error: ' + e.error);
    } else {
        coords1 = e.coords.longitude;
        coords1 = e.coords.latitude;
    }
  });
  } else {

    alert('Please enable location services');
  } 


  var url = "http://www.mywebsite.com/api/return-latlong/"+coords1+"/"+coords2;
  var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // this function is called when data is returned from the server and available for use
        // this.responseText holds the raw text return of the message (used for text/JSON)
        // this.responseXML holds any returned XML (including SOAP)
        // this.responseData holds any returned binary data
        Ti.API.debug(this.responseText);
        alert('success');
    },
    onerror: function(e) {
        // this function is called when an error occurs, including a timeout
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout:5000  // in milliseconds
   });
   xhr.open("GET", url);
   xhr.send();  // request is actually sent with this statement


  alert(url);
</code></pre>