Google Maps API v3 adding an InfoWindow to each marker

前端 未结 9 565
刺人心
刺人心 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:18

    Hey everyone. I don't know if this is the optimal solution but I figured I'd post it here to hopefully help people out in the future. Please comment if you see anything that should be changed.

    My for loops is now:

    for (var i in tracks[racer_id].data.points) {
        values = tracks[racer_id].data.points[i];                
        point = new google.maps.LatLng(values.lat, values.lng);
        if (values.qst) {
            tracks[racer_id].markers[i] = add_marker(racer_id, point, 'Speed: ' + values.inst + ' knots
    Invalid: '); } track_coordinates.push(point); bd.extend(point); }

    And add_marker is defined as:

    var info_window = new google.maps.InfoWindow({content: ''});
    
    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 = marker.note;
            info_window.open(map, marker);
        });
        return marker;
    }
    

    You can use info_window.close() to turn off the info_window at any time. Hope this helps someone.

提交回复
热议问题