【问题标题】:Google map cluster custom image谷歌地图集群自定义图片
【发布时间】:2018-03-08 06:10:56
【问题描述】:

我想用自定义图像更改谷歌地图聚类。但是,它不会改变我提供的任何东西。这个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
              }
            ]}

【问题讨论】:

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


    【解决方案1】:

    加载这些图像有一种特定的方式,这是因为链接到示例代码的 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'.
    

    检查你的控制台,我确定你会得到这样的东西:

    GET 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

    【讨论】:

    • 我尝试做 imagePath='/static/images/cluster' 并添加了 m1,m2,m3....png。但是,它似乎没有改变图像也没有抛出 404
    • 您能否编辑您的帖子以显示minimal, complete, and verifiable example,这样我可以帮助您在一个孤立的示例中进行调试。还要确保你有 5 张图片。
    • 我可以通过添加样式对象来更改图像。感谢@henrisycip的帮助
    【解决方案2】:

    简单的解决方案可以是在图片 url 后面加上问号。

    imagePath: '<image_url>?'
    

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

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

    【讨论】:

      【解决方案3】:

      作为替代方案,您可以将“&z=”(z 可以是您想要的任何参数名称)附加到图像 URL 的末尾,这样您就可以使用任何您喜欢的图像。

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

      【讨论】:

        猜你喜欢
        • 2011-11-10
        • 2017-06-13
        • 1970-01-01
        • 1970-01-01
        • 2014-07-29
        • 2020-12-18
        • 2021-11-10
        • 2019-01-20
        • 2013-03-24
        相关资源
        最近更新 更多