Google map API v3 - Add multiple infowindows

后端 未结 2 1866
你的背包
你的背包 2020-12-05 10:50

I\'ve got a working section of google maps javascript, I did have a problem.

Now the issue I had was that only one infowindow was showing up, the la

2条回答
  •  借酒劲吻你
    2020-12-05 11:26

    For some reason, I had to modify marker object and access that data inside event lisener function.

    Scope of click event fuction on marker is marker object.

    var infowindow = new google.maps.InfoWindow();
    
    for(int i = 0; i < locations.length; i++){
        var latLng = new google.maps.LatLng(locations[i][1], locations[i][2]);
        var contentString = locations[i][0];
    
        marker = new google.maps.Marker({           
              position: latLng,
              map: map,
              contentString: contentString
        });
        marker.data = locations[i]; // adds object to marker object
    
    
        marker.addListener('click', function() {
            // read custom data in this.data   
            infowindow.setContent("
    "+ this.data[0] +"
    "); infowindow.open(map, this); map.setCenter(this.getPosition()); }); }

提交回复
热议问题