Search address on google maps

Asked

Viewed 184 times

-3

I have to put on my site a search of addresses where the user type the address and shows the place typed and the address of the site.

How to do it?

1 answer

1

Use the service of Geocoding of the Google Maps API for this.

Take their implementation example, it’s very easy to understand:

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 8,
    center: {lat: -34.397, lng: 150.644}
  });
  var geocoder = new google.maps.Geocoder();

  document.getElementById('submit').addEventListener('click', function() {
    geocodeAddress(geocoder, map);
  });
}

function geocodeAddress(geocoder, resultsMap) {
  var address = document.getElementById('address').value;
  geocoder.geocode({'address': address}, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      resultsMap.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
        map: resultsMap,
        position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
  • Thank you !!

  • You are welcome! If the answer solved your problem, validate it with an ok. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.