Marker content (infoWindow) Google Maps

前端 未结 2 1394
傲寒
傲寒 2020-12-07 20:53

I\'m trying to add infoWindow\'s to multiple markers on a Google Map. The closest I have come is to get an infoWindow to display the last address you can see in the array, o

相关标签:
2条回答
  • 2020-12-07 21:22

    Although this question has already been answered, I think this approach is better : http://jsfiddle.net/kjy112/3CvaD/ extract from this question on StackOverFlow google maps - open marker infowindow given the coordinates:

    Each marker gets an "infowindow" entry :

    function createMarker(lat, lon, html) {
        var newmarker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lon),
            map: map,
            title: html
        });
    
        newmarker['infowindow'] = new google.maps.InfoWindow({
                content: html
            });
    
        google.maps.event.addListener(newmarker, 'mouseover', function() {
            this['infowindow'].open(map, this);
        });
    }
    
    0 讨论(0)
  • 2020-12-07 21:32

    We've solved this, although we didn't think having the addListener outside of the for would make any difference, it seems to. Here's the answer:

    Create a new function with your information for the infoWindow in it:

    function addInfoWindow(marker, message) {
    
                var infoWindow = new google.maps.InfoWindow({
                    content: message
                });
    
                google.maps.event.addListener(marker, 'click', function () {
                    infoWindow.open(map, marker);
                });
            }
    

    Then call the function with the array ID and the marker you want to create:

    addInfoWindow(marker, hotels[i][3]);
    
    0 讨论(0)
提交回复
热议问题