Repeating infowindow on markers

0

I'm working with the Google Map API and after adding the infowindow in the bookmarks, I noticed that they are repeating themselves, but I could not resolve them.

Can anyone take a look at the code below and tell me what's going on? I'm loading into a Bootstrap modal.

Code:

$().click(function(e) {
    ...
    // data <- é um json com algo mais ou menos assim: 
    // {[lat: 21.030300,long: 23.003002,tag: 'XXTO'],[lat: 20.22100,long: 21.113002,tag: 'NNTO']}
    var map = new google.maps.Map(document.getElementById('map-canvas'), {
        zoom: 17,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i,pos;

    for (var item in data)      
    {                       
        pos = new google.maps.LatLng(data[item].Lat,data[item].Long);

        marker = new google.maps.Marker({
            position: pos,
            map: map,
            icon: '/prj/assets/imgs/lamp.png'
        });

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent('Tag n°:' + data[item].tag);
                infowindow.open(map, marker);
            } 
        })(marker, i));
    }

    $("#modalMaps").on("shown.bs.modal", function(e) {
        google.maps.event.trigger(map, "resize");
        return map.setCenter(pos);
    });
    $("#modalMaps").modal();
}
    
asked by anonymous 14.11.2014 / 16:31

1 answer

1

The problem is in the following snippet:

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent('Tag n°:' + data[item].tag);
            infowindow.open(map, marker);
        } 
    })(marker, i));

In the above excerpt I passed the variable i to the method, but I did not have any i to work on the code, hence I solved by changing the parameter path like this:

     google.maps.event.addListener(marker, 'click', (function(marker, valor) {
        return function() {
            infowindow.setContent('Tag n°:' + valor);
            infowindow.open(map, marker);
        } 
    })(marker, data[item].tag));

So I solved the problem.

    
14.11.2014 / 17:40