-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update just one specific marker #563
Comments
Hi, It looks like many people start using MCG for real-time status data! :-) (referring to #561, #555 and #535) Obviously the marker should reflect the new status, even if the latter changed while the marker was clustered. Furthermore, in many cases you would like a cluster to reflect somehow the status of its children markers, so you need a way to re-draw its icon (as also referred to in #498). Definitely +1 for me as well. |
All, Seems to be quite simple to implement. /**
* Changes the class of all supplied markers and updates the clusters.
* @param aMarkers array|L.LayerGroup|L.MarkerClusterGroup of markers to be modified.
* @param sNewClassOrFn string|function new class to be applied on markers,
* either as a string or as a function that receives the old class as argument.
*/
updateMarkersClass: function (aMarkers, sNewClassOrFn) {
if (aMarkers instanceof L.MarkerClusterGroup) {
aMarkers = aMarkers.getAllChildMarkers();
} else if (aMarkers instanceof L.LayerGroup) {
var array = [];
for (var j in aMarkers._layers) {
array.push(aMarkers._layers[j]);
}
aMarkers = array;
} else if (!(aMarkers.length)) {
aMarkers = [aMarkers];
}
// Make a function that returns always the same string if needed.
if (typeof sNewClassOrFn !== "function") {
sNewClassOrFn = function () {
return sNewClassOrFn;
}
}
var marker,
i = 0,
icon,
parent;
for(; i < aMarkers.length; i += 1) {
marker = aMarkers[i];
icon = marker.options.icon;
// Update icon class.
icon.options.className = sNewClassOrFn(icon.options.className);
// Force Marker redraw.
marker.setIcon(icon);
// Set parent clusters' icon dirty, all the way up.
parent = marker.__parent;
while (parent) {
parent._iconNeedsUpdate = true;
parent = parent.__parent;
}
}
// Refresh visible markers.
this._featureGroup.eachLayer(function (c) {
if (c instanceof L.MarkerCluster && c._iconNeedsUpdate) {
c._updateIcon();
}
});
} If looks good I will try to make a PR when I have time. |
@ghybs Do not copy-paste patches into comments, do pull requests instead. Right now it's nearly impossible to see the diff against the old code. |
Hi, @IvanSanchez thanks for your comment, I certainly understand your point. I will certainly make a PR when I have time. @Quentin22, your need is certainly doable, you should be able to modify my code to change the iconUrl of your standard L.Icon, instead of my code modifying the class of an L.DivIcon. Hope this helps. |
My bad, line 3 after comments: aMarkers = aMarkers.getAllChildMarkers(); should have been: aMarkers = aMarkers._topClusterLevel.getAllChildMarkers(); I tried to tidy up and split the code in PR #564. Hopefully it should be easier to understand and use. |
Thank you for you code ! But I don't how to use it, sorry... For now I create a geographic coordinates of one marker present on my map and change his color with an event by removing layer and adding layer again with juste one marker updated : var lat = 10.574222078332806,
lng = -68.62060546875;
function clickBtn() {
map.removeLayer(markers);
var geo = L.geoJson(geojson, {
onEachFeature: function (feature, layer) {
var popupText = 'Label : ' + feature.properties.name;
layer.bindPopup(popupText);
if(feature.geometry.type == "Point"){
if(feature.geometry.coordinates[0] == lng && feature.geometry.coordinates[1] == lat) {
layer.setIcon(RedIcon);
}
else {
layer.setIcon(BlueIcon);
}
}
}
markers = new L.MarkerClusterGroup();
markers.addLayer(geo);
map.addLayer(markers);
} But I still don't know how to change color of cluster which contains this updated marker. |
Hi, All right, in fact you seem to need first basic instructions on how to update a marker in a layerGroup, independently from MarkerClusterGroup, or even from L.GeoJSON.
If you need further help, I recommend you using Stack Overflow. You would probably get help quicker for what you need. |
Thank you so much for your response. I'm going to focus on that and I will let you know if I can do something, or not =). |
Hi Quentin, I just realized there is a forum for Leaflet: https://groups.google.com/forum/#!forum/leaflet-js |
Hi @Quentin22, please can you kindly close this issue, now that you seem to have succeeded in making it work? |
Hello,
Context :
I have a nodejs server to serve tiles with .mbtiles file and I use MarkerClusterGroup on my client to display markers with a .geojson file which represent a state of communication. Markers are .png red or blue according to their state of communication. I try to have an interactive map which display markers and show if the communication is OK (blue.png) or if the communication is down (red.png).
Issue :
I want to be able to get one specific marker by his geographic coordinates or by his name (in .geojson file and on popupText) and change the color of it by changing the .png (red or blue). On cascade, I wish to change the color of cluster parent of this marker in red for example.
Is it possible to do that without removing layer with all markers and add again this layer with function "removeLayer" and "addLayer" ? I just wand to change one specific marker.
Thank you !
Quentin
The text was updated successfully, but these errors were encountered: