How to add the address to infowindow gmaps v3?

0

I have a collection of points where I can see all of them on the map! I would like to add an infowindow "balloon" to each of them by entering the full address of each coordinate.

    function initialize() {


        var latlng = new google.maps.LatLng(latitudes, longitudes);

        var options = {
            zoom: 12,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("mapa"), options);
        geocoder = new google.maps.Geocoder();

        var pontos = [];
        if (coord.length > 1) {
            var adress = [];
            for (var i = 0; i < coord.length; i++) {
                var location = coord[i].split(",");

                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(location[0], location[1]),
                    map: map,

                })
            }
        }
  }
        initialize();
    
asked by anonymous 06.05.2015 / 14:41

1 answer

1

You basically need three steps. First, start the InfoWindow classes for the balloon and Geocoder to search the address from the coordinates:

var infoWindow = new google.maps.InfoWindow();
var geocoder = new google.maps.Geocoder();

And then, within your for loop, add a listener to the Marker open this InfoWindow with a click and its contents:

google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent("Carregando...");
    infoWindow.open(map, this);
});

And even within this listener , shortly after opening the InfoWindow , start the reverse geocoding :

google.maps.event.addListener(infoWindow, 'domready', function() {
    geocoder.geocode({'latLng': infoWindow.position}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            infoWindow.setContent(results[0].formatted_address);
        }
    });
});

That is, as soon as InfoWindow is opened (so I used another listener , domready ) and content "Loading ... ", I looked up the address from the coordinate and then I changed the content to the formatted address that came in the result.

    
07.05.2015 / 15:20