首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google映射数组标记聚类

问Google映射数组标记聚类
EN

Stack Overflow用户
提问于 2014-05-11 03:16:07
回答 1查看 1.3K关注 0票数 1

我知道已经有很多关于这个问题的问题了--但我读过很多问题,但在我的特殊情况下似乎什么都做不到。我想用一堆显示澳大利亚各地商店位置的信息窗口制作一张地图(<--这部分我正在工作).当放大标记时,应按城市分组,并显示一个数字.此外,我想要一个html的链接列表,当点击缩放到地图上的一个特定的标记。很像这里显示的示例:example.html

下面是我的代码(现在我刚刚在两个城市中添加了位置,稍后在全国有许多城市):

代码语言:javascript
复制
<!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链接..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-11 08:00:08

数组“站点”在您的代码中没有定义,我想知道为什么.但我不是javascript的专家..。如果在initialize()函数中移动它,它将正确工作。

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

在您的setMarkers()函数中,您还声明了一个局部变量"sites",这是很危险的,因为您还有一个具有相同名称的全局变量。(自从我把它移到initialize()中以后,情况就不再是这样了)

最后,我不知道您打算如何处理信息图,现在,没有显示任何内容,但是如果您编写infowindow.setContent('Hello Marker');而不是"this.html",它就会工作。

您可以在这里找到代码的一个工作示例:http://jsfiddle.net/839dV/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23588522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档