\n
code snippet:
\nvar gm_map;\nvar markerArray = [];\nvar clusterMarkers = [];\nvar infoWindow = new google.maps.InfoWindow();\n\nfunction initialize(searchMarkers = []) {\n var marker, i;\n\n if (searchMarkers.length == 0) {\n clusterMarkers = [\n new google.maps.Marker({\n position: new google.maps.LatLng(59.381059, 13.504026),\n map: gm_map,\n title: \"P1220214 1.JPG\",\n label: {\n text: 'Label 1',\n color: '#222222',\n fontSize: '12px'\n }\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.381059, 13.504026),\n map: gm_map,\n title: \"P1220214 2.JPG\",\n label: {\n text: 'Label 2',\n color: '#222222',\n fontSize: '12px'\n }\n\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.340715, 13.49631),\n map: gm_map,\n title: \"P1220214 3.JPG\",\n label: {\n text: 'Label 3',\n color: '#222222',\n fontSize: '12px'\n }\n\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.327232, 13.487384),\n map: gm_map,\n title: \"P1220214 4.JPG\",\n label: {\n text: 'Label 4',\n color: '#222222',\n fontSize: '12px'\n }\n\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.379034, 13.516566),\n map: gm_map,\n title: \"P1220214 5.JPG\",\n label: {\n text: 'Label 5',\n color: '#222222',\n fontSize: '12px'\n }\n\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.330000, 13.485000),\n map: gm_map,\n title: \"P1220214 6.JPG\",\n label: {\n text: 'Label 6',\n color: '#222222',\n fontSize: '12px'\n }\n\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.328501, 13.485782),\n map: gm_map,\n title: \"P1220214 7.JPG\",\n label: {\n text: 'Label 7',\n color: '#222222',\n fontSize: '12px'\n }\n\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.328501, 13.485782),\n map: gm_map,\n title: \"P1220214 8.JPG\",\n label: {\n text: 'Label 8',\n color: '#222222',\n fontSize: '12px'\n }\n\n })\n ]\n } else {\n clusterMarkers = searchMarkers;\n }\n\n var options_googlemaps = {\n minZoom: 9,\n zoom: 9,\n center: new google.maps.LatLng(59.328631, 13.485688),\n maxZoom: 18,\n }\n\n gm_map = new google.maps.Map(document.getElementById('map'), options_googlemaps)\n\n var options_markerclusterer = {\n //gridSize: 20,\n maxZoom: 18,\n zoomOnClick: false,\n imagePath: 'https://www.geocodezip.net/mapIcons/markerclusterer/m',\n label: {\n text: 'This is custom text'\n }\n };\n\n var markerCluster = new MarkerClusterer(gm_map, clusterMarkers, options_markerclusterer);\n var mcStyles = markerCluster.getStyles();\n console.log(mcStyles);\n mcStyles.push({\n url: \"https://www.geocodezip.net/mapIcons/markerclusterer/heart50.png\",\n width: 50,\n height: 44,\n anchorIcon: [44, 25],\n textSize: 10,\n textColor: \"black\",\n textDecoration: \"none\",\n // textLineHeight: 12,\n // fontWeight: \"bold\",\n fontStyle: \"normal\",\n fontFamily: \"Arial,sans-serif\",\n backgroundPosition: \"0 0\",\n });\n markerCluster.setCalculator(function(markers, numStyles) {\n console.log(\"setCalculator(numMarkers\" + markers.length + \", numStyles=\" + numStyles + \")\");\n var index = 0,\n count = markers.length,\n total = count;\n\n while (total !== 0) {\n //Create a new total by dividing by a set number\n total = parseInt(total / 5, 10);\n //Increase the index and move up to the next style\n index++;\n }\n index = Math.min(index, numStyles);\n samePin = _getPinStatus(markers);\n if (samePin) {\n return {\n text: count + \" (\" + count + \" Jobs)\",\n index: 6\n };\n } else {\n return {\n text: count,\n index: index\n };\n }\n });\n\n\n google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {\n\n //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.\n var markers = cluster.getMarkers();\n var samePin = false;\n samePin = _getPinStatus(markers);\n\n if (!samePin) {\n gm_map.setCenter(cluster.center_);\n gm_map.setZoom(gm_map.getZoom() + 2);\n } else {\n // This is where I want to change the icon of the particular cluster. \n var array = [];\n for (i = 0; i < markers.length; i++) {\n array.push(markers[i].getTitle() + '<br>');\n }\n if (gm_map.getZoom() <= markerCluster.getMaxZoom()) {\n infoWindow.setContent(markers.length + \" markers<br>\" + array);\n infoWindow.setPosition(cluster.getCenter());\n infoWindow.open(gm_map);\n }\n }\n });\n\n for (i = 0; i < clusterMarkers.length; i++) {\n var marker = clusterMarkers[i];\n\n google.maps.event.addListener(marker, 'click', (function(marker) {\n return function() {\n infoWindow.setContent(this.getTitle());\n infoWindow.open(gm_map, this);\n }\n })(marker));\n }\n}\n\n$(document).ready(function() {\n initialize();\n $('body').on('click', '#close-link', function() {\n $('#toggle-photolist').fadeOut();\n $('#close-overlay').fadeOut();\n });\n $('#refreshmap').click(function() {\n searchMarkers = [\n new google.maps.Marker({\n position: new google.maps.LatLng(59.381059, 13.504026),\n map: gm_map,\n title: \"Search Data 1.JPG\",\n label: {\n text: 'Search label 1',\n color: '#222222',\n fontSize: '12px'\n }\n\n }),\n\n new google.maps.Marker({\n position: new google.maps.LatLng(59.338683, 13.492057),\n map: gm_map,\n title: \"Search Data 2.JPG\",\n label: {\n text: 'Search label 1',\n color: '#222222',\n fontSize: '12px'\n }\n\n })\n ]\n initialize(searchMarkers);\n });\n});\n\nfunction _getPinStatus(markers) {\n for (i = 0; i < markers.length; i++) {\n var parentLatitude = markers[0].getPosition().lat();\n var parentLongitude = markers[0].getPosition().lng();\n\n if (markers[i].getPosition().lat() == parentLatitude &&\n markers[0].getPosition().lng() == parentLongitude) {\n samePin = true;\n } else {\n // break out of the loop in case a single pin is different and allow user to further zoom in the map\n samePin = false;\n break;\n }\n }\n return samePin;\n}
\r\n/* Always set the map height explicitly to define the size of the div\n * element that contains the map. */\n\n#map {\n height: 100%;\n}\n\n\n/* Optional: Makes the sample page fill the window. */\n\nhtml,\nbody {\n height: 95%;\n margin: 0;\n padding: 0;\n}
\r\n<!DOCTYPE html>\n<html>\n\n<head>\n <title>SO MarkerClusterer Question</title>\n <script src=\"https://polyfill.io/v3/polyfill.min.js?features=default\"></script>\n <script src=\"https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=&v=weekly\"></script>\n <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js\"></script>\n <script src=\"https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js\"></script>\n <!-- jsFiddle will insert css and js -->\n</head>\n\n<body>\n <button type=\"button\" id=\"refreshmap\">Click Me!</button>\n <div id=\"map\"></div>\n</body>\n\n</html>
\r\ndetails on how this works:
\nMarkerClustererPlus source on GitHub
\n\n\n\n/**
\n
\n* Sets the icon styles to the appropriate element in the styles array.
\n*
\n* @ignore
\n* @param sums The icon label text and styles index.
\n*/
\nuseStyle(sums: ClusterIconInfo): void
Is used in the Cluster updateIcon:
\n\n\n/**
\n
\n* Updates the cluster icon.
\n*/
\npublic updateIcon(): void
here: Cluster
\n\n\nconst sums = this.markerClusterer_.getCalculator()(\nthis.markers_,\nnumStyles\n);\nthis.clusterIcon_.setCenter(this.center_);
\n
\nthis.clusterIcon_.useStyle(sums);
\nthis.clusterIcon_.show();
Where sums
is the structure returned by the calculator function:
Reputation: 332
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>
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.
I also somehow could not manage to get the zoom change
event - but this is rather a separate query
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.
I could not find details around setStyles
and its implementation. Would it be feasible to push styles once the map is already plotted.
Appreciate any suggestions.
Upvotes: 0
Views: 6752
Reputation: 161404
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",
});
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
};
}
});
code snippet:
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>
details on how this works:
MarkerClustererPlus source on GitHub
/**
* 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
Is used in the Cluster updateIcon:
/**
* Updates the cluster icon.
*/
public updateIcon(): void
here: Cluster
const sums = this.markerClusterer_.getCalculator()( this.markers_, numStyles ); this.clusterIcon_.setCenter(this.center_);
this.clusterIcon_.useStyle(sums);
this.clusterIcon_.show();
Where sums
is the structure returned by the calculator function:
Upvotes: 2