开发者

Change the content of infowindow in Google Maps

开发者 https://www.devze.com 2023-03-19 22:18 出处:网络
Based on a selected country and date a list of results will show up on the map. The placements of markers with html开发者_StackOverflow data in them works fine, the problem I have is handling the case

Based on a selected country and date a list of results will show up on the map. The placements of markers with html开发者_StackOverflow data in them works fine, the problem I have is handling the cases with duplicate markers.

I found a way to check for duplicate ones, but I would like to be able to modify the content in the infowindow of the one already present. So that I can add the content of the duplicate marker to the existing marker that stands in the same location.

The problem I'm having is that I can't find the right way to access the data in the existing infowindow for the duplicate one.

If I do alert(infoWindow.getContent());, then I just get "undefined". I also tried markers[i].getContent(), but that also didn't do anything.

Any idea's, or suggestion on how I can access the infowindows of previously added markers, get their content with getContent(), and add the data of the duplicate one to it?

var marker = new google.maps.Marker({
    map: map,
    position: latlng
});

for (var i=0; i<markers.length; i++) {
    if (markers[i].getPosition().equals(marker.getPosition())) {
        alert('duplicate found');
        alert(infoWindow.getContent());

    } else {
        google.maps.event.addListener(marker, 'click', function() {
            infoWindow.setContent(html);
            infoWindow.open(map, marker);
        });
    }
}


I am pretty sure you can't do exactly what you want.

As far as I am aware, and can see in the API, neither the infowindow or the marker knows anything about eachother. The only thing that knows which infowindow should be shown when clicking a certain marker is the google maps eventhandler.

A solution to your problem would then be to store all the infowindows in an array, and the markers in another array where you would make sure that the index of the corresponding markers and infowindows where the same.

Another way to do it would be to define your own object where you make sure it can hold both a marker and a infowindow.

In any way, you would have to loop through the markers array to find the duplicate marker and then get the corresponding infowindow by using one of the two scenarios I have described. It seems a bit silly, but I am not aware of another way to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消