2

我已经阅读了该网站的数百篇帖子——所有帖子都非常有帮助,但我被困在了许多其他帖子的地方——关闭问题。我的地图加载,我的标记加载,但臭名昭著的信息窗口总是显示最后一条记录的数据。

http://harvest.cals.ncsu.edu/applications/kim_test/map/map_test.cfm

这是我的代码:

<!DOCTYPE html>
<html> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
    <title>Google Maps Multiple Markers</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" 
        type="text/javascript"></script>
    </head> 
<body>
    <div id="map" style="width: 700px; height: 700px;"></div>
    <script type="text/javascript">
        var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(35.8189, -78.6447),
        mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        <cfquery name="alumniMap" datasource="mydatasource" maxrows="10">
        Select MapLatitude, mapLongitude, ID, entity_name
        From tbl_jeffScholarsAlumni
        Order by ID
        </cfquery>

        <cfset p = 1>

        <cfloop query="alumniMap" >
            <cfoutput>
                var #toScript(alumniMap.mapLatitude, "latitude")#;
                var #toScript(alumniMap.mapLongitude, "longitude")#;
                var #toScript(alumniMap.ID, "ncsuID")#;
                var #toScript(alumniMap.entity_name, "ncsupeep")#;
                var #toScript(p, "thisMark")#;          
            </cfoutput> 
            var locations = [
                ['this is me ' + ncsupeep + '...' , latitude, longitude, thisMark],
            ];
            <cfset p = p + 1>

            var infowindow = new google.maps.InfoWindow();
            var marker, i;

            for (i = 0; i < locations.length; i++) 
            {  
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                    map: map
                });
            }

            for (i = 0; i < locations.length; i++) {
                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][0]);
                    infowindow.open(map, marker);
                }
                })(marker, i));
            }

        </cfloop>
    </script>
</body>
</html>
4

1 回答 1

1

修改此代码:

for (i = 0; i < locations.length; i++) {
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(locations[i][0]);
            infowindow.open(map, marker);
        }
    })(marker, i));
}

至:

for (i = 0; i < locations.length; i++) {
    bindInfoWindow(marker, map, infowindow, locations[i][0]);
}

添加一个新函数,如下所示:

function bindInfoWindow(marker, map, infowindow, strDescription) {
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(strDescription);
        infowindow.open(map, marker);
    });
}
于 2013-10-02T15:38:29.590 回答