title: geolocation set variable to current location
body: <p>Im trying to get the current position of the user into an object variable but it keeps resetting on it self.</p>
  
  <pre><code>var UserCoords = { lat: '', lng: '' };
  function getLocation() {
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(showPosition);
  
      } else {
          alert("Geolocation is not supported by this browser.");
      }
  }
  
  function showPosition(position) {
      UserCoords.lat = position.coords.latitude;
      UserCoords.lng = position.coords.longitude;
      console.log(UserCoords);//Works
  }
  
  function getUserCoords() {
      getLocation();
      console.log(UserCoords);//Returns lat and lng empty
      OtherFunction(UserCoords.lat, UserCoords.lng);
  }
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>function getLocation() {
      if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function (position) {
              UserCoords.lat = position.coords.latitude;
              UserCoords.lng = position.coords.longitude;
              console.log(UserCoords);//Works
          });
  }
  </code></pre>
  
  <p>But neither works.</p>
  
  <p>I want to do this because i need to use the users position in some other functions and i would like to not have to call geolocation every time.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>jsfiddle: <a href="http://jsfiddle.net/9fvcqcvz" rel="nofollow">http://jsfiddle.net/9fvcqcvz</a></p>