Google会为数组映射标记群集

时间:2014-05-11 03:16:08

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

我知道关于这个问题已经有很多问题了 - 但是我已经阅读了很多这些问题但似乎无法在我的特定情况下得到任何工作。我想制作一张带有大量自定义标记的地图,信息窗口显示澳大利亚各地的商店位置(< - 这部分我已经工作了)...... 当缩小时,标记应该按城市分组,并显示一个数字...我也想要一个链接的HTML链接列表,点击后缩放到地图上的特定标记。与此处显示的示例非常相似:https://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclustererplus/examples/speed_test_example.html

下面是我的代码(目前我刚刚在两个城市添加了位置,后来在全国范围内有很多城市):

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Locations</title>
    <meta charset="utf-8">
    <script src="js/jquery-1.7.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.14/src/markerclusterer_packed.js"></script>
<script type="text/javascript">
var infowindow = null;
    $(document).ready(function () { initialize();  });

    function initialize() {

        var centerMap = new google.maps.LatLng(-25.274398, 133.775136);

        var myOptions = {
            zoom: 4,
            center: centerMap,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        var map = new google.maps.Map(document.getElementById("map"), myOptions);
        var mc = new MarkerClusterer(map);

        setMarkers(map, sites);
        infowindow = new google.maps.InfoWindow({
                content: "loading..."
            });

    }

    var sites = [
    ['location 16', -37.814107 , 144.96328, 6, 'Description in info window','images/map_mobile.png'],
    ['location 5', -37.911394 , 145.189584, 5, 'Description in info window','images/map_store.png'],
    ['location 4', -33.963542 , 151.134273, 4, 'Description in info window','images/map_store.png'],
    ['location 3', -33.923960 , 150.921158, 3, 'Description in info window','images/map_store.png'],
    ['location 2', -34.065619 , 150.796491, 2, 'Description in info window','images/map_mobile.png'],
    ['location 1', -33.752178 , 150.6910478, 1, 'Description in info window','images/map_mobile.png']
];

    function setMarkers(map, markers) {

        for (var i = 0; i < markers.length; i++) {
            var sites = markers[i];
            var siteLatLng = new google.maps.LatLng(sites[1], sites[2]);
            var marker = new google.maps.Marker({
                position: siteLatLng,
                map: map,
                title: sites[0],
                zIndex: sites[3],
                html: sites[4],
                icon: sites[5]
            });

            google.maps.event.addListener(marker, "click", function () {
                //alert(this.html);
                infowindow.setContent(this.html);
                infowindow.open(map, this);
            });
        }
    }
</script>
</head>
<body onload="initialize()">
<div id="map" style="width: 620px; height: 600px;"></div>
</body>
</html>

总而言之,我需要帮助才能使聚类工作和html链接......

1 个答案:

答案 0 :(得分:2)

你的代码中未定义数组“sites”,我想知道为什么......但我不是javascript的专家...... 如果您在initialize()功能中移动它,它可以正常工作。

MarkerClusterer的声明不包含任何标记,这根本没有意义。基于此处的示例https://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/docs/examples.html,您可以看到您需要首先声明您的标记,然后将它们包含在MarkerClusterer的声明中。

同样在setMarkers()函数中,您声明了一个局部变量“sites”,这很危险,因为您还有一个具有相同名称的全局变量。 (因为我在initialize()

中移动它,所以情况不再如此

最后,我不知道你打算用infowindow做什么,现在没有显示任何内容,但如果你写infowindow.setContent('Hello Marker');而不是“this.html”,它将会起作用。

您可以在此处找到代码的工作示例:http://jsfiddle.net/839dV/