如何在谷歌地图API中创建多个信息窗口

18

我在地图上创建了3个标记,它们的数据来自于一个json文件。但是当我点击标记并尝试打开信息窗口时,只有最后一个标记会正确响应.. 请帮帮我.... 这是代码....

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
    <script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js" type="text/javascript"></script>
</head>
<body>
    <script>
        var json = [
            {
                "title": "Stockholm",
                "lat": 59.3,
                "lng": 18.1,
                "description": "Stockholm is the capital and the largest city of Sweden and constitutes the most populated urban area in Scandinavia with a population of 2.1 million in the metropolitan area (2010)"
            },
            {
                "title": "Oslo",
                "lat": 59.9,
                "lng": 10.8,
                "description": "Oslo is a municipality, and the capital and most populous city of Norway with a metropolitan population of 1,442,318 (as of 2010)."
            },
            {
                "title": "Copenhagen",
                "lat": 55.7,
                "lng": 12.6,
                "description": "Copenhagen is the capital of Denmark and its most populous city, with a metropolitan population of 1,931,467 (as of 1 January 2012)."
            }
        ]
        var myCenter=new google.maps.LatLng(51.508742,-0.120850);
        function initialize()
        {
            var mapProp = {
                center:myCenter,
                zoom:5,
                mapTypeId:google.maps.MapTypeId.ROADMAP
            };
            var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
            for (var i = 0, length = json.length; i < length; i++) {
                var data=json[i];
                var latLng = new google.maps.LatLng(data.lat, data.lng); 
                // Creating a marker and putting it on the map
                var marker = new google.maps.Marker({
                    position: latLng,
                    map: map,
                    title: data.title
                });
            } 
                var infowindow = new google.maps.InfoWindow({
                    content: data.description
                });

            google.maps.event.addListener(marker, 'click', function() {    
                infowindow.open(map,marker);
            });

        }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>
    <div id="googleMap" style="width:100%;height:100%;"></div>
</body>

</html>
1个回答

42

问题在于,如果你在循环内尝试将内容分配给信息窗口。然后当你点击标记时,它只知道来自循环的最后一次迭代的内容。你需要使用闭包代替。有几种不同的处理方法,这是我通常使用的方法:

function initialize() {
    var mapProp = {
        center:myCenter,
        zoom:5,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };
    var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);

    var infowindow =  new google.maps.InfoWindow({
        content: ""
    });

    for (var i = 0, length = json.length; i < length; i++) {
        var data=json[i];
        var latLng = new google.maps.LatLng(data.lat, data.lng); 
        // Creating a marker and putting it on the map
        var marker = new google.maps.Marker({
            position: latLng,
            map: map,
            title: data.title
        });

        bindInfoWindow(marker, map, infowindow, data.description);
    } 
}

function bindInfoWindow(marker, map, infowindow, description) {
    marker.addListener('click', function() {
        infowindow.setContent(description);
        infowindow.open(map, this);
    });
}

有没有想法为什么在这个例子中信息窗口根本不会弹出?标记在我的地图上都正确显示,但是当我单击标记时设置在infowindow.setContent(strDescription);中的数据并没有显示。 - gjw80
@gjw80 你可能需要提出你自己的问题,这样我们才能看到你的所有代码,并帮助诊断问题。 - duncan
如果您有兴趣查看,我在这里创建了一个:http://stackoverflow.com/questions/19635172/infowindows-not-displaying-any-information?noredirect=1#comment29152145_19635172 - gjw80

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接