【问题标题】:Add custom image to Marker Cluster without overwriting markers将自定义图像添加到标记集群而不覆盖标记
【发布时间】:2023-06-01 20:53:01
【问题描述】:

我有以下添加地图标记集群的 jQuery 代码:

markerCluster = new MarkerClusterer(map);

我正在使用 markerCluster.addMarker(Marker); 将单个标记添加到集群(页面底部的完整代码块)。

我的问题是我的集群默认没有附加图像。

我可以做到以下几点:

var options = 'an image path';
markerCluster = new MarkerClusterer(map,markers,options);

但是,由于 options 是第三个参数,我有效地覆盖了第二个参数。有没有办法在不覆盖标记的情况下将图像设置为 MarkerCluster?

(function () {
            var address = response[key]["address"];
            $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+response[key]["post_code"]+'&sensor=false', null, function (data) {
                var p = data.results[0].geometry.location
                var latlng = new google.maps.LatLng(p.lat, p.lng);
                var Marker = new google.maps.Marker({
                    position: latlng,
                    map: map,
                    content: address
                });
                markerCluster.addMarker(Marker);
                google.maps.event.addListener(Marker, 'click', function () {    
                    infowindow.setContent(this.content);
                    infowindow.open(map, this);
                });
            });
        })();

【问题讨论】:

标签: jquery google-maps google-maps-api-3


【解决方案1】:

默认情况下,MarkerClusterer../images 文件夹中查找标记(相对于markerclusterer.js 的位置)。

对您来说最简单的方法是从github 获取图像文件夹,因此您的文件结构如下:

-images
  -m1.png
  -m2.png
  -..etc other files from the directory
-SOME_FOLDER //can be lib, libraries, what have you
  -markerclusterer.js

或者,您可以使用options 对象的imagePath 属性,这样您就会有这样的东西:

var options = { imagePath: "PATH_TO_IMAGES_FOLDER/m" };
markerCluster = new MarkerClusterer(map, markers, options);

【讨论】:

    最近更新 更多