Google Maps API v3 adding an InfoWindow to each marker

前端 未结 9 557
刺人心
刺人心 2020-11-29 21:36

NOTE: I\'m using v3 of the Google Maps API

I\'m trying to add an info window to each marker I put on the map. Currently I\'m doing this with the following code:

9条回答
  •  生来不讨喜
    2020-11-29 22:10

    The add_marker still has a closure issue, cause it uses the marker variable outside the google.maps.event.addListener scope.

    A better implementation would be:

    function add_marker(racer_id, point, note) {
        var marker = new google.maps.Marker({map: map, position: point, clickable: true});
        marker.note = note;
        google.maps.event.addListener(marker, 'click', function() {
            info_window.content = this.note;
            info_window.open(this.getMap(), this);
        });
        return marker;
    }
    

    I also used the map from the marker, this way you don't need to pass the google map object, you probably want to use the map where the marker belongs to anyway.

提交回复
热议问题