Repeating infowindow on bookmarks

Asked

Viewed 320 times

0

I am working with the Google Map API and after adding the infowindow on the markers, I noticed that they are repeating, but I could not solve.

Can someone take a look at the code below and tell me what’s going on? I’m clicking on 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();
}
  • See http://meta.pt.stackoverflow.com/questions/1084/como-devemos-formatar-questions-respostas/1297#1297 e http://meta.pt.stackoverflow.com/questions/846/sauda%C3%A7%C3%B5es-e-acknowledgments

1 answer

1


The problem is in the following section:

    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 section I passed the variable i for the method, but had no i to work on the code, hence, I decided to change the passing of parameters so:

     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.

Browser other questions tagged

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