JavaScript   

Get latitude and longitude from Google places autocomplete

This short code snippet is about getting the latitude and longitude of the selected address from places autocomplete.
Note – Don’t forget to include the following script tag in your webpage before the given code snippet.

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_HERE&libraries=places"></script>
  function getPlace(){
    const input = document.getElementById("fieldID");
    const options = {
      //add options here if you want more customizations
    };
    const autocomplete = new google.maps.places.Autocomplete(input, options);
    google.maps.event.addListener(autocomplete, 'place_changed', function () {
      let place = autocomplete.getPlace();
      console.log(place.geometry.location.lat());
      console.log(place.geometry.location.lng());
    });
  }
  window.addEventListener('load', getPlace);
Need a helping hand in fixing your website issues?

If you are facing any problems in implementing these code snippets and tutorials, you can hire us to fix your website issues.

Hire Us