admin管理员组

文章数量:1401602

Using Google Maps API to add an infoWindow to each marker. Markers e from an array.

Although, infoWindow only shows up for the first marker, not the others. Why? Thanks.

function set_markers(array) {
    var mapOptions = {
        zoom: 13
    }
    for (var i = 0; i < array.length; i++) {
        var single_location = array[i];
        var myLatLng = new google.maps.LatLng(single_location[1], single_location[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: single_location[0]
        });
        var infowindow = new google.maps.InfoWindow({
            content: ""
        });
    }

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<h3>'+this.title+'</h3>');
        infowindow.open(map,this);
    });
}

Using Google Maps API to add an infoWindow to each marker. Markers e from an array.

Although, infoWindow only shows up for the first marker, not the others. Why? Thanks.

function set_markers(array) {
    var mapOptions = {
        zoom: 13
    }
    for (var i = 0; i < array.length; i++) {
        var single_location = array[i];
        var myLatLng = new google.maps.LatLng(single_location[1], single_location[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: single_location[0]
        });
        var infowindow = new google.maps.InfoWindow({
            content: ""
        });
    }

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent('<h3>'+this.title+'</h3>');
        infowindow.open(map,this);
    });
}
Share Improve this question asked Jan 21, 2015 at 14:40 trstrs 8632 gold badges17 silver badges36 bronze badges 3
  • Are you showing the actual code here? Looks like infowindow is undefined in your call to addListener - or is it a global not shown? – Stephen O'Connor Commented Jan 21, 2015 at 14:59
  • @SteveO'Connor "var infowindow" is defined inside the "for" loop. Is that what you're looking for? – trs Commented Jan 21, 2015 at 15:17
  • @murid you're creating your markers and infowindows within a loop. But then you only have a single event listener on your markers, i.e. only on the very last one. Move the event listener inside the loop – duncan Commented Jan 21, 2015 at 15:42
Add a ment  | 

1 Answer 1

Reset to default 7
var infowindow = new google.maps.InfoWindow();

function set_markers(array) {

    var mapOptions = {
        zoom: 13
    };

    for (var i = 0; i < array.length; i++) {

        var single_location = array[i];
        var myLatLng = new google.maps.LatLng(single_location[1], single_location[2]);
        var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: single_location[0]
        });

        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent('<h3>' + this.title + '</h3>');
            infowindow.open(map, this);
        });
    }
}

This is untested since you didn't post a MCVE.

本文标签: javascriptMultiple quotInfoWindowquot for array of quotmarkersquot on Google MapsStack Overflow