谷歌地图集群自定义图像

时间:2017-09-26 21:41:07

标签: javascript reactjs google-maps react-google-maps

我想用自定义图片更改谷歌地图群集。但是,它不会改变我提供的任何内容。这个initMap函数是

  

https://developers.google.com/maps/documentation/javascript/marker-clustering

我试图用谷歌的一些随机图像来改变聚类图像。 但是,它不会呈现任何内容。

群集不支持自定义群集映像?

function initMap() {

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 3,
          center: {lat: -28.024, lng: 140.887}
        });

        // Create an array of alphabetical characters used to label the markers.
        var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        // Add some markers to the map.
        // Note: The code uses the JavaScript Array.prototype.map() method to
        // create an array of markers based on a given "locations" array.
        // The map() method here has nothing to do with the Google Maps API.
        var markers = locations.map(function(location, i) {
          return new google.maps.Marker({
            position: location,
            label: labels[i % labels.length]
          });
        });

        // Add a marker clusterer to manage the markers.
        var markerCluster = new MarkerClusterer(map, markers,
            {imagePath: 'http://www.empowervate.org/wp-content/uploads/2015/11/circle.jpg'});
      }

感谢您的帮助@henrisycip

我可以通过提供样式选项来更改群集图像,如下所示。我不确定为什么imagePath什么都不做..

 styles={[
              {
                url: "/static/images/cluster/m1.png",
                height: 53,
                width: 53
              },
              {
                url: "/static/images/cluster/m2.png",
                height: 53,
                width: 53
              },
              {
                url: "/static/images/cluster/m3.png",
                height: 53,
                width: 53
              },
              {
                url: "/static/images/cluster/m4.png",
                height: 53,
                width: 53
              },
              {
                url: "/static/images/cluster/m5.png",
                height: 53,
                width: 53
              }
            ]}

3 个答案:

答案 0 :(得分:4)

由于与样本代码相关联的markerclusterer.js库,这些图片的加载方式和特定方式。

它将查看您提供的imagePath,但会对其进行迭代,查找image1,image2,image3等。这就是为什么https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m的默认路径有效,因为它将开始寻找m1.png,m2.png,m3.png等。

这是图书馆的一部分,表示它正在这样做:

'imagePath': (string) The base URL where the images representing
 *                  clusters will be found. The full URL will be:
 *                  {imagePath}[1-5].{imageExtension}
 *                  Default: '../images/m'.

检查您的控制台,我确定您收到的内容如下:

  

获取http://www.empowervate.org/wp-content/uploads/2015/11/circle.jpg1.png 404(未找到)

您需要知道的一切实际上都在documentation上。您将看到有下载markerclusterer.js,m1.png,m2.png等副本的说明,只需将路径更改为您自己的文件夹结构。

您的图片路径示例如下: imagePath: 'http://www.empowervate.org/wp-content/uploads/2015/11/circle' 只要你有circle1.png,circle2.png等

您也可以查看上一个问题:Add custom image to Marker Cluster without overwriting markers

答案 1 :(得分:2)

简单的解决方案是在图像 url 后附加问号。

imagePath: '<image_url>?'

另一种解决方案是设置自定义样式,例如

  new MarkerClusterer(map, marker, {
    styles: [
      {
        url: '<image_url>'
      }
    ]
  })

答案 2 :(得分:0)

作为替代方案,您可以仅在图像URL的末尾附加'&z ='(z可以是您想要的任何参数名称),它将允许您使用任何喜欢的图像。

这将防止自动附加的[[1 | 2 | 3] .png”更改您的URL。

相关问题