title: Get address of a location using Geolocation and google maps
body: <p>There's a form on my webpage which is supposed to get the address of the user in a formfield.</p>
  
  <p>When the user clicks allow on the location prompt my purpose is to get the address of the user in an input box in the form.</p>
  
  <p>The prompt comes but this code is unable to fetch the address of the user.</p>
  
  <p>I am looking for something like <a href="https://geeks.ae/dubai/personal-services/" rel="nofollow noreferrer">this</a></p>
  
  <p>Here's my code</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;form id="contact" action="" method="post" align="center"&gt;
  
  &lt;fieldset&gt;
    &lt;input placeholder="Your Address" id="address" type="text" tabindex="1" required autofocus&gt;
  &lt;/fieldset&gt;
  &lt;fieldset&gt;
    &lt;button name="submit" type="submit" id="contact-submit" data-submit="...Sending"&gt;Submit&lt;/button&gt;
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>
  Javascript</p>
  
  <pre><code>&lt;script src="https://code.jquery.com/jquery-1.10.1.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function () {
  
  var currgeocoder;
  
  //Set geo location lat and long
  navigator.geolocation.getCurrentPosition(function (position, html5Error) {
      geo_loc = processGeolocationResult(position);
      currLatLong = geo_loc.split(",");
      initializeCurrent(currLatLong[0], currLatLong[1]);
  });
  
  //Get geo location result
  function processGeolocationResult(position) {
      html5Lat = position.coords.latitude; //Get latitude
      html5Lon = position.coords.longitude; //Get longitude
      html5TimeStamp = position.timestamp; //Get timestamp
      html5Accuracy = position.coords.accuracy; //Get accuracy in meters
      return (html5Lat).toFixed(8) + ", " + (html5Lon).toFixed(8);
  }
  
  //Check value is present or
  function initializeCurrent(latcurr, longcurr) {
      currgeocoder = new google.maps.Geocoder();
  
      console.log(latcurr + "-- ######## --" + longcurr);
  
      if (latcurr != '' &amp;&amp; longcurr != '') {
          //call google api function
          var myLatlng = new google.maps.LatLng(latcurr, longcurr);
          return getCurrentAddress(myLatlng);
      }
  }
  
  //Get current address
  function getCurrentAddress(location) {
      currgeocoder.geocode({
          'location': location
      }, function (results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
              console.log(results[0]);
              $("#address").html(results[0].formatted_address);
          } else {
              alert('Geocode was not successful for the following reason: ' + status);
          }
      });
  }
  });
  </code></pre>
  
  <p></p>