更改特定Google地图集群的集群图标-markerclusterplus [英] Change cluster icon for a particular google map cluster - markerclusterplus
问题描述
我想根据某些特定条件更改群集图标(动态/针对特定群集).我能够确定情况,并限制了对它的进一步深入研究.在该特定级别,我们正在为用户打开信息框.
I would like to change the cluster icon (dynamically/for particular cluster) based on some particular condition. I am able to identify the condition and have restricted further drill-down on the same. At that particular level we are opening infobox for the user.
问题出在群集上,因为我想将图标更改为其他名称.对于单个图钉,图标就可以了.
The problem lies with cluster as I want to change the icon to something else. For the individual pins the icon is fine.
下面是我要实现的代码:
Below is the code that I am trying to implement:
<!DOCTYPE html>
<html>
<head>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 80%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCtTgoZ3tcsUSNfK6yCPzX_muSVOG6N9Ho"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<button type="button" id="refreshmap">Click Me!</button>
<div id="map"></div>
<script>
var gm_map;
var markerArray = [];
var clusterMarkers = [];
var infoWindow = new google.maps.InfoWindow();
function initialize(searchMarkers = []) {
var marker, i;
if(searchMarkers.length == 0) {
clusterMarkers = [
new google.maps.Marker({
position: new google.maps.LatLng(59.381059, 13.504026),
map: gm_map,
title: "P1220214 1.JPG",
label: {
text: 'Label 1',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.381059, 13.504026),
map: gm_map,
title: "P1220214 2.JPG",
label: {
text: 'Label 2',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.340715, 13.49631),
map: gm_map,
title: "P1220214 3.JPG",
label: {
text: 'Label 3',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.327232, 13.487384),
map: gm_map,
title: "P1220214 4.JPG",
label: {
text: 'Label 4',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.379034, 13.516566),
map: gm_map,
title: "P1220214 5.JPG",
label: {
text: 'Label 5',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.330000, 13.485000),
map: gm_map,
title: "P1220214 6.JPG",
label: {
text: 'Label 6',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.328501, 13.485782),
map: gm_map,
title: "P1220214 7.JPG",
label: {
text: 'Label 7',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.328501, 13.485782),
map: gm_map,
title: "P1220214 8.JPG",
label: {
text: 'Label 8',
color: '#222222',
fontSize: '12px'
}
})
]
} else {
clusterMarkers = searchMarkers;
}
var options_googlemaps = {
minZoom: 9,
zoom: 9,
center: new google.maps.LatLng(59.328631, 13.485688),
maxZoom: 18,
}
gm_map = new google.maps.Map(document.getElementById('map'), options_googlemaps)
var options_markerclusterer = {
//gridSize: 20,
maxZoom: 18,
zoomOnClick: false,
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',
label: {
text: 'This is custom text'
}
};
var markerCluster = new MarkerClusterer(gm_map, clusterMarkers, options_markerclusterer);
markerCluster.setCalculator(function(markers, numStyles){
var index = 0,
count = markers.length,
total = count;
while (total !== 0) {
//Create a new total by dividing by a set number
total = parseInt(total / 5, 10);
//Increase the index and move up to the next style
index++;
}
index = Math.min(index, numStyles);
samePin = _getPinStatus(markers);
if (samePin) {
return {
text: count + " ("+ count + " Jobs)",
index: index
};
} else {
return {
text: count,
index: index
};
}
});
google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
//This needs some logic to identify if all the internal pings are at same location - execute else part otherwise further zoom in using if code.
var markers = cluster.getMarkers();
var samePin = false;
samePin = _getPinStatus(markers);
if (!samePin ){
gm_map.setCenter(cluster.center_);
gm_map.setZoom(gm_map.getZoom() + 2);
} else {
// This is where I want to change the icon of the particular cluster.
var array = [];
for (i = 0; i < markers.length; i++) {
array.push(markers[i].getTitle() + '<br>');
}
if (gm_map.getZoom() <= markerCluster.getMaxZoom()) {
infoWindow.setContent(markers.length + " markers<br>" + array);
infoWindow.setPosition(cluster.getCenter());
infoWindow.open(gm_map);
}
}
});
for (i = 0; i < clusterMarkers.length; i++) {
var marker = clusterMarkers[i];
google.maps.event.addListener(marker, 'click', (function(marker) {
return function() {
infoWindow.setContent(this.getTitle());
infoWindow.open(gm_map, this);
}
})(marker));
}
}
$(document).ready(function() {
initialize();
$('body').on('click', '#close-link', function() {
$('#toggle-photolist').fadeOut();
$('#close-overlay').fadeOut();
});
$('#refreshmap').click(function() {
searchMarkers = [
new google.maps.Marker({
position: new google.maps.LatLng(59.381059, 13.504026),
map: gm_map,
title: "Search Data 1.JPG",
label: {
text: 'Search label 1',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.338683, 13.492057),
map: gm_map,
title: "Search Data 2.JPG",
label: {
text: 'Search label 1',
color: '#222222',
fontSize: '12px'
}
})
]
initialize(searchMarkers);
});
});
function _getPinStatus(markers) {
for (i = 0; i < markers.length; i++) {
var parentLatitude = markers[0].getPosition().lat();
var parentLongitude = markers[0].getPosition().lng();
if(markers[i].getPosition().lat() == parentLatitude &&
markers[0].getPosition().lng() == parentLongitude) {
samePin = true;
}
else {
// break out of the loop in case a single pin is different and allow user to further zoom in the map
samePin = false;
break;
}
}
return samePin;
}
</script>
<!-- Replace following script src -->
</body>
</html>
我要构建的逻辑是,如果群集下面有相同的引脚(我不想向下钻取),则将图标更改为其他任何内容.
函数_getPinStatus
帮助我识别以上逻辑并创建信息窗口.
The logic that I am trying to build is that if the cluster has identical pins beneath it (I would not like to drill down) then change the icon to anything else.
The function _getPinStatus
helps me identify the above logic and creates the infowindow.
我也以某种方式无法获得zoom change
事件-但这是一个单独的查询
I also somehow could not manage to get the zoom change
event - but this is rather a separate query
我尝试使用clearMarkers()
函数,并通过在options_markerclusterer
数组中提供图像来重新映射群集,但是它确实替换了所有基础标记上的图像.
I tried with clearMarkers()
function and remap the clusters by providing the image in options_markerclusterer
array but it does replace the image on all underlying markers.
我找不到有关setStyles
及其实现的详细信息.地图绘制完成后,推送样式是否可行.
I could not find details around setStyles
and its implementation. Would it be feasible to push styles once the map is already plotted.
赞赏任何建议.
推荐答案
您可以在用于定义群集"图标样式的样式数组中添加其他样式:
You can add additional styles to the style array used to define the Cluster icon styles:
var mcStyles = markerCluster.getStyles();
mcStyles.push({
url: "http://www.geocodezip.com/mapIcons/markerclusterer/heart50.png",
width: 50,
height: 44,
anchorIcon: [44, 25],
textSize: 10,
textColor: "black",
textDecoration: "none",
fontStyle: "normal",
fontFamily: "Arial,sans-serif",
backgroundPosition: "0 0",
});
然后调整计算器"当您点击相同的图钉"时,返回适当的索引(在本例中为6)情况:
Then adjust the "calculator" to return the appropriate index (in this case 6) when you hit your "same pin" case:
markerCluster.setCalculator(function(markers, numStyles) {
var index = 0,
count = markers.length,
total = count;
while (total !== 0) {
//Create a new total by dividing by a set number
total = parseInt(total / 5, 10);
//Increase the index and move up to the next style
index++;
}
index = Math.min(index, numStyles);
samePin = _getPinStatus(markers);
if (samePin) {
return {
text: count + " (" + count + " Jobs)",
index: 6 // reference added style
};
} else {
return {
text: count,
index: index
};
}
});
代码段:
var gm_map;
var markerArray = [];
var clusterMarkers = [];
var infoWindow = new google.maps.InfoWindow();
function initialize(searchMarkers = []) {
var marker, i;
if (searchMarkers.length == 0) {
clusterMarkers = [
new google.maps.Marker({
position: new google.maps.LatLng(59.381059, 13.504026),
map: gm_map,
title: "P1220214 1.JPG",
label: {
text: 'Label 1',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.381059, 13.504026),
map: gm_map,
title: "P1220214 2.JPG",
label: {
text: 'Label 2',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.340715, 13.49631),
map: gm_map,
title: "P1220214 3.JPG",
label: {
text: 'Label 3',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.327232, 13.487384),
map: gm_map,
title: "P1220214 4.JPG",
label: {
text: 'Label 4',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.379034, 13.516566),
map: gm_map,
title: "P1220214 5.JPG",
label: {
text: 'Label 5',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.330000, 13.485000),
map: gm_map,
title: "P1220214 6.JPG",
label: {
text: 'Label 6',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.328501, 13.485782),
map: gm_map,
title: "P1220214 7.JPG",
label: {
text: 'Label 7',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.328501, 13.485782),
map: gm_map,
title: "P1220214 8.JPG",
label: {
text: 'Label 8',
color: '#222222',
fontSize: '12px'
}
})
]
} else {
clusterMarkers = searchMarkers;
}
var options_googlemaps = {
minZoom: 9,
zoom: 9,
center: new google.maps.LatLng(59.328631, 13.485688),
maxZoom: 18,
}
gm_map = new google.maps.Map(document.getElementById('map'), options_googlemaps)
var options_markerclusterer = {
//gridSize: 20,
maxZoom: 18,
zoomOnClick: false,
imagePath: 'https://www.geocodezip.net/mapIcons/markerclusterer/m',
label: {
text: 'This is custom text'
}
};
var markerCluster = new MarkerClusterer(gm_map, clusterMarkers, options_markerclusterer);
var mcStyles = markerCluster.getStyles();
console.log(mcStyles);
mcStyles.push({
url: "https://www.geocodezip.net/mapIcons/markerclusterer/heart50.png",
width: 50,
height: 44,
anchorIcon: [44, 25],
textSize: 10,
textColor: "black",
textDecoration: "none",
// textLineHeight: 12,
// fontWeight: "bold",
fontStyle: "normal",
fontFamily: "Arial,sans-serif",
backgroundPosition: "0 0",
});
markerCluster.setCalculator(function(markers, numStyles) {
console.log("setCalculator(numMarkers" + markers.length + ", numStyles=" + numStyles + ")");
var index = 0,
count = markers.length,
total = count;
while (total !== 0) {
//Create a new total by dividing by a set number
total = parseInt(total / 5, 10);
//Increase the index and move up to the next style
index++;
}
index = Math.min(index, numStyles);
samePin = _getPinStatus(markers);
if (samePin) {
return {
text: count + " (" + count + " Jobs)",
index: 6
};
} else {
return {
text: count,
index: index
};
}
});
google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
//This needs some logic to identify if all the internal pings are at same location - execute else part otherwise further zoom in using if code.
var markers = cluster.getMarkers();
var samePin = false;
samePin = _getPinStatus(markers);
if (!samePin) {
gm_map.setCenter(cluster.center_);
gm_map.setZoom(gm_map.getZoom() + 2);
} else {
// This is where I want to change the icon of the particular cluster.
var array = [];
for (i = 0; i < markers.length; i++) {
array.push(markers[i].getTitle() + '<br>');
}
if (gm_map.getZoom() <= markerCluster.getMaxZoom()) {
infoWindow.setContent(markers.length + " markers<br>" + array);
infoWindow.setPosition(cluster.getCenter());
infoWindow.open(gm_map);
}
}
});
for (i = 0; i < clusterMarkers.length; i++) {
var marker = clusterMarkers[i];
google.maps.event.addListener(marker, 'click', (function(marker) {
return function() {
infoWindow.setContent(this.getTitle());
infoWindow.open(gm_map, this);
}
})(marker));
}
}
$(document).ready(function() {
initialize();
$('body').on('click', '#close-link', function() {
$('#toggle-photolist').fadeOut();
$('#close-overlay').fadeOut();
});
$('#refreshmap').click(function() {
searchMarkers = [
new google.maps.Marker({
position: new google.maps.LatLng(59.381059, 13.504026),
map: gm_map,
title: "Search Data 1.JPG",
label: {
text: 'Search label 1',
color: '#222222',
fontSize: '12px'
}
}),
new google.maps.Marker({
position: new google.maps.LatLng(59.338683, 13.492057),
map: gm_map,
title: "Search Data 2.JPG",
label: {
text: 'Search label 1',
color: '#222222',
fontSize: '12px'
}
})
]
initialize(searchMarkers);
});
});
function _getPinStatus(markers) {
for (i = 0; i < markers.length; i++) {
var parentLatitude = markers[0].getPosition().lat();
var parentLongitude = markers[0].getPosition().lng();
if (markers[i].getPosition().lat() == parentLatitude &&
markers[0].getPosition().lng() == parentLongitude) {
samePin = true;
} else {
// break out of the loop in case a single pin is different and allow user to further zoom in the map
samePin = false;
break;
}
}
return samePin;
}
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 95%;
margin: 0;
padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>SO MarkerClusterer Question</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=&v=weekly"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<!-- jsFiddle will insert css and js -->
</head>
<body>
<button type="button" id="refreshmap">Click Me!</button>
<div id="map"></div>
</body>
</html>
详细说明其工作方式:
useStyle :
/**
*将图标样式设置为样式数组中的适当元素.
*
* @ignore
* @param sums图标标签的文本和样式索引.
*/
useStyle(总和: ClusterIconInfo ):void
/**
* Sets the icon styles to the appropriate element in the styles array.
*
* @ignore
* @param sums The icon label text and styles index.
*/
useStyle(sums: ClusterIconInfo): void
用于集群
/** /** 此处:群集 const sums = this.markerClusterer_.getCalculator()(
this.markers_,
numStyles
);
this.clusterIcon_.setCenter(this.center_); const sums = this.markerClusterer_.getCalculator()(
this.markers_,
numStyles
);
this.clusterIcon_.setCenter(this.center_); 其中 Where 这篇关于更改特定Google地图集群的集群图标-markerclusterplus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
*更新群集图标.
*/
公共updateIcon():void
* Updates the cluster icon.
*/
public updateIcon(): void
this.clusterIcon_.useStyle(sums);
this.clusterIcon_.show();
this.clusterIcon_.useStyle(sums);
this.clusterIcon_.show();sums
是计算器函数返回的结构:sums
is the structure returned by the calculator function: