【问题标题】:Cluster google map pins when using an object of markers instead of an array使用标记对象而不是数组时集群谷歌地图引脚
【发布时间】:2013-06-12 15:02:30
【问题描述】:

我正在管理这个网站:http://www.gprprojecten.nl 基本上我正在做的是从网络服务获取数据流并对其进行处理。之后我对这些数据做了各种各样的事情,包括在谷歌地图上显示所有项目。 问题是,随着项目数量的增长,(自定义)引脚开始重叠(不可见,不可点击)。

所以我想把我的图钉聚集起来。

我以A32.blog 为起点开始研究a google maps clusterer library

但是现在出现了一个问题。由于我选择了一个名为 filter.js 的客户端过滤器系统,因此我依赖于使用谷歌地图的这种方式,它使用对象而不是集群器库中使用的一组标记。

这是我用来生成地图的代码,并与 filter.js 结合使用:

var googleMap = {
center_lat_lng: [52.14, 5.3],
map: null,
markers: {},
addMarker: function (project) {
    var that = this;
    var image = new google.maps.MarkerImage('@Url.Content("~/Content/img/marker.png")',
        new google.maps.Size(63, 27),
        new google.maps.Point(0, 0),
        new google.maps.Point(23, 27));
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(project.loclat, project.loclong),
        map: this.map,
        title: project.gebouwnaam,
        icon: image
    });

    marker.info_window_content = "<div class=\"gmapcontentwindow\"><h4>" + project.gebouwnaam + "</h4><img src=\"" + project.thumburl + "\" alt=\"" + project.gebouwnaam + "\" /><div class=\"gebouwdata\"><span class=\"date\">" + project.publicatiedatum + "</span><h5>" + project.plaats + "</h5><span>" + project.gebruiksfunctie + " | " + project.gebouwcategorie + "</span><span>" + project.licentiehouder + "</span><span class=\"stars " + project.rating + "\"></span>"
    if (project.akkoorddoorexpert == "True") {
        marker.info_window_content += "<span class=\"expert\"></span>"
    }
    var forwardUrl = '@Url.Content("~/Home/Details/")';
    marker.info_window_content += "<span  class=\"forward\"><a href=\"" + forwardUrl + project.id + "\" title=\"Bekijk " + project.gebouwnaam + " in detail\">Lees meer >></b></a></span></div></div>"
    this.markers[project.id] = marker

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

updateMarkers: function (filtering_result) {
    var google_map = this;
    $.each(google_map.markers, function () { this.setMap(null); })
    $.each(filtering_result, function () {
        google_map.markers[this.id].setMap(google_map.map);
    });
},

init: function () {
    var options = {
        center: new google.maps.LatLng(this.center_lat_lng[0], this.center_lat_lng[1]),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    this.map = new google.maps.Map(document.getElementById("gmap"), options)

    this.infowindow = new google.maps.InfoWindow({

    });
    //var markersarray = $(this.markers).toArray();
    //console.log(markersarray);
    //doet het nog niet???
    //http://a32.me/2012/05/handling-huge-amount-of-markers-on-google-maps-with-clusters/
    //var markerCluster = new MarkerClusterer(this.map, markersarray, {
    //  gridSize: 10,
    //  minimumClusterSize: 2
    //});
}
};

project 是一个从 filter.js 传递给 init 函数的对象

【问题讨论】:

  • 我不明白你的问题。如果您可以访问标记(作为 google.maps.Marker 对象),则可以将它们添加到集群器(单独,如果不是通过数组)。
  • 好吧,我也不明白,但我似乎无法将它们放入集群器,对于初学者来说,我应该在哪里调用集群器,在 init 函数中?
  • 您的意思是MarkerClusterer.addMarker 电话?还是创建 MarkerClusterer 对象?
  • 后者,如果我只是从 document.ready 调用它似乎可以工作,但是我无法在过滤器回调中访问它(并修改它)
  • 那么你需要在全局范围内声明它(或者在你的对象中保留对它的引用)。

标签: google-maps-api-3 markerclusterer


【解决方案1】:

您需要将 Marker Clusterer 添加到全局范围或在您的对象中保留对它的引用。

【讨论】:

    猜你喜欢
    • 2013-03-24
    • 1970-01-01
    • 2013-12-13
    • 2023-03-20
    • 1970-01-01
    • 2016-04-03
    • 2013-01-13
    • 1970-01-01
    相关资源
    最近更新 更多