How to add the address to the infowindow Gmaps v3?

Asked

Viewed 475 times

0

I have a collection of dots where I see all of them on the map! would like to add an infowindow "balloon" to each of them informing 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();
  • http://gmap3.net/en/catalog/10-overlays/infowindow-35

1 answer

1


You need basically three steps. First, start the classes InfoWindow to 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 inside your noose for, add a Listener to the Marker open this InfoWindow with a click and its content:

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

And still inside this Listener, right after opening the InfoWindow, initiate the call from 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, so the InfoWindow was opened (so I used another Listener, the domready) and with the content "Carrying...", I searched the address from the coordinate and then switched the contents to the formatted address that came in the result.

  • within the setContent(''); I wanted to add more information, only that this information is inside an array and I can’t get the values that are in each array ,within this function google.maps.Event.addListener ,does not recognize the i++ of the loop that is above the function.

  • I don’t understand. There’s a way to put this piece of code somewhere, like in Pastebin?

  • I can make another post?

  • infowindow.setContent(Results[0].formatted_address+'Data:' +data[i]);//the index i is not working.

  • For each map marketing I have a different infowindow.setContent content. Only q these different values are inside an array or each position is information used in the former Marker: marker1=data[1];//So to understand a little more.

Browser other questions tagged

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