javascript - 谷歌地图集群自定义图像

标签 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
              }
            ]}

最佳答案

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

您需要知道的一切实际上都在 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

关于javascript - 谷歌地图集群自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46436284/

相关文章:

javascript - NodeJS 发送混洗数据作为 API 响应

javascript - 如何获取数组中的对象以显示在文档或控制台上

javascript - Axios - 未捕获( promise )错误 : Request failed with status code 500

javascript - 为什么 Jest 尝试解析我的 index.ts 中的每个组件?

jquery - 如何使用 jQuery 访问多维 JSON 的值并将它们绘制在 Google map 上

javascript - OVER_QUERY_LIMIT google map API v3

javascript - 将输入元素对转换为 json

javascript - 如何处理 jqPlot 内存泄漏?

javascript - 如何设置react组件的iframe内容

javascript - 更改谷歌地图API路线规划器语言