Search code examples
google-mapse-commercereverse-geocodingstreet-address

Getting Address from Google Map (Reverse Geocoding)


I have a online shop and people buy products and we send a products to their address , but sometimes customers enter a bad address and we couldn't find the destination.

I want to show a Google map in address form then customer locate their address on the map and finally address of that point fetched from Google map.

Is Google offers this feature?


Solution

  • Yes, this service is known as reverse geocoding. And it's actually quite simple to implement. Assuming you get lat and long values from map click event, like so:

    var map;
    function initialize() {
      var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
      var myOptions = {
        zoom: 4,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
      google.maps.event.addListener(map, 'click', function(event) {
        getAddress(event.latLng);
      });
    }
    
    function getAddress(location latlng) {
     var geocoder;
      var map;
      var infowindow = new google.maps.InfoWindow();
      var marker;
      function initialize() {
        geocoder = new google.maps.Geocoder();
    
        var myOptions = {
          zoom: 8,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      }
    }