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