如何将单独的infoWindows添加到多个谷歌地图标记

时间:2013-08-21 23:38:34

标签: javascript google-maps google-maps-api-3

我正在尝试在移动电话应用中为多个地图标记添加单独的信息窗口,但收效甚微。当我运行我的应用程序并单击任何标记时,相同的标记每次都会打开其infoWindow。不知道我哪里出错了。

            result.users.forEach(function(entry){
                latLng = new google.maps.LatLng(entry.user_lat, entry.user_lon);
                marker.push(new google.maps.Marker({map: map, position: latLng}));
                circle.push(new google.maps.Circle({map: map, radius: entry.user_accuracy, strokeWeight:1, strokeOpacity:0.5, fillOpacity:0.2, fillColor: '#AA0000'}));
                circle[circle.length-1].bindTo('center', marker[marker.length-1], 'position');
                info.push(new google.maps.InfoWindow({content: '<b>Phone ID:</b> ' + entry.user_uuid + '<br /><b>Callsign:</b> ' + entry.user_callsign + '<br /><b>Time:</b> ' + entry.user_datestamp}));
                google.maps.event.addListener(marker[marker.length-1], 'click', function() {info[info.length-1].open(map, marker[marker.length-1]);});
                if (entry.user_uuid == device.uuid){
                    map.setCenter(latLng);
                }
            });

1 个答案:

答案 0 :(得分:0)

打开信息时,必须更新infoWindows的内容,否则显示最后的内容。

只需修改addListener:

google.maps.event.addListener(marker, 'click', function () {
    info.setOptions({
        content: MarkerContent
    });
    info.open(map, marker);
});