1

I would like to have a radio button to show the cluster which the first radio button: show clustering.The problem of my project is when I click at the first time, it shows clustering correctly, but when choose it for second time it shows again which clustering overlap each other (do not delete the first clustering)

    var command = L.control({position: 'topright'});
    command.onAdd = function(map){
    var div = L.DomUtil.create('div', 'command');
    div.innerHTML = '<input id="command" type="radio" name="abc">'  
    document.getElementById ("command").addEventListener ("click",init,false) 

    function init(){
          $.getJSON("XYpoint2.geojson",function(data){
          var clusters;
          var point = L.geoJson(data,{
              var marker1 = L.marker(latlng, {icon: myIcon1});
              return marker1;
          });
         clusters = L.markerClusterGroup();
            clusters.addLayer(point);
            map.addLayer(clusters);
    };

How can I delete the previous cluster when I click the radio button a second time?

2

The code you posted is incomplete and quite strange. I've never seen this usage of L.geoJson.

Basically, you need to remove the cluster layer if you don't want to see it anymore and add another one :

// This variable will contain the current cluster layer
var clusters;
var command = L.control({position: 'topright'});
command.onAdd = function(map){}
var div = L.DomUtil.create('div', 'command');
div.innerHTML = '<input id="command" type="radio" name="abc">'
document.getElementById ("command").addEventListener ("click",init,false)

function init(){
    $.getJSON("XYpoint2.geojson",function(data){
        // Here you remove the layer
        if (clusters) {
            map.removeLayer(clusters);
        }
        var point = L.geoJson(data,{
            var marker1 = L.marker(latlng, {icon: myIcon1});
            return marker1;
        });
        clusters = L.markerClusterGroup();
        clusters.addLayer(point);
        map.addLayer(clusters);
    };
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.