Changing the style of a leaflet cluster when pressing it












1















I have created a map with clusters created like so:



//create clustering markers
var markers = L.markerClusterGroup({
spiderfyOnMaxZoom: false,
showCoverageOnHover: false,
zoomToBoundsOnClick: false,
singleMarkerMode: true, //makes sure that single incidents looks the same as clusters (but are still treated as single markers)
iconCreateFunction: defineClusterIcon
});

var layer_group = L.geoJSON(geoJson);


markers.addLayer(layer_group);
map.addLayer(markers);
map.fitBounds(markers.getBounds());


In the defineClusterIcon function, I create a SVG which then is converted to HTML and defines the icon:



return L.divIcon({ 
iconSize: new L.Point(40, 45),
html: html,
classname: 'leaflet-div-icon'
});


I now want to be able to change the style of the cluster (or marker, which also is styled as a cluster), when pressing it - and I want it to return to the original styling when pressed again.



Instead of changing the style of the actual svg elements, I am thinking that it might be easier to just change the style of the class:



.leaflet-div-icon {
background: rgba(0,0,0,0);
border: none;
}


Where I then want to have a border when the cluster/marker has been pressed. I do not know, whether it is possible to change the class within the on clusterclick or click functions, or if it can be done in another way.



My code, as it is now can be found here - where the wanted effect also can be seen on the controls on the right side: http://bl.ocks.org/skov94/f006cd45d2daa2bc67e4f514774fdd0d










share|improve this question



























    1















    I have created a map with clusters created like so:



    //create clustering markers
    var markers = L.markerClusterGroup({
    spiderfyOnMaxZoom: false,
    showCoverageOnHover: false,
    zoomToBoundsOnClick: false,
    singleMarkerMode: true, //makes sure that single incidents looks the same as clusters (but are still treated as single markers)
    iconCreateFunction: defineClusterIcon
    });

    var layer_group = L.geoJSON(geoJson);


    markers.addLayer(layer_group);
    map.addLayer(markers);
    map.fitBounds(markers.getBounds());


    In the defineClusterIcon function, I create a SVG which then is converted to HTML and defines the icon:



    return L.divIcon({ 
    iconSize: new L.Point(40, 45),
    html: html,
    classname: 'leaflet-div-icon'
    });


    I now want to be able to change the style of the cluster (or marker, which also is styled as a cluster), when pressing it - and I want it to return to the original styling when pressed again.



    Instead of changing the style of the actual svg elements, I am thinking that it might be easier to just change the style of the class:



    .leaflet-div-icon {
    background: rgba(0,0,0,0);
    border: none;
    }


    Where I then want to have a border when the cluster/marker has been pressed. I do not know, whether it is possible to change the class within the on clusterclick or click functions, or if it can be done in another way.



    My code, as it is now can be found here - where the wanted effect also can be seen on the controls on the right side: http://bl.ocks.org/skov94/f006cd45d2daa2bc67e4f514774fdd0d










    share|improve this question

























      1












      1








      1








      I have created a map with clusters created like so:



      //create clustering markers
      var markers = L.markerClusterGroup({
      spiderfyOnMaxZoom: false,
      showCoverageOnHover: false,
      zoomToBoundsOnClick: false,
      singleMarkerMode: true, //makes sure that single incidents looks the same as clusters (but are still treated as single markers)
      iconCreateFunction: defineClusterIcon
      });

      var layer_group = L.geoJSON(geoJson);


      markers.addLayer(layer_group);
      map.addLayer(markers);
      map.fitBounds(markers.getBounds());


      In the defineClusterIcon function, I create a SVG which then is converted to HTML and defines the icon:



      return L.divIcon({ 
      iconSize: new L.Point(40, 45),
      html: html,
      classname: 'leaflet-div-icon'
      });


      I now want to be able to change the style of the cluster (or marker, which also is styled as a cluster), when pressing it - and I want it to return to the original styling when pressed again.



      Instead of changing the style of the actual svg elements, I am thinking that it might be easier to just change the style of the class:



      .leaflet-div-icon {
      background: rgba(0,0,0,0);
      border: none;
      }


      Where I then want to have a border when the cluster/marker has been pressed. I do not know, whether it is possible to change the class within the on clusterclick or click functions, or if it can be done in another way.



      My code, as it is now can be found here - where the wanted effect also can be seen on the controls on the right side: http://bl.ocks.org/skov94/f006cd45d2daa2bc67e4f514774fdd0d










      share|improve this question














      I have created a map with clusters created like so:



      //create clustering markers
      var markers = L.markerClusterGroup({
      spiderfyOnMaxZoom: false,
      showCoverageOnHover: false,
      zoomToBoundsOnClick: false,
      singleMarkerMode: true, //makes sure that single incidents looks the same as clusters (but are still treated as single markers)
      iconCreateFunction: defineClusterIcon
      });

      var layer_group = L.geoJSON(geoJson);


      markers.addLayer(layer_group);
      map.addLayer(markers);
      map.fitBounds(markers.getBounds());


      In the defineClusterIcon function, I create a SVG which then is converted to HTML and defines the icon:



      return L.divIcon({ 
      iconSize: new L.Point(40, 45),
      html: html,
      classname: 'leaflet-div-icon'
      });


      I now want to be able to change the style of the cluster (or marker, which also is styled as a cluster), when pressing it - and I want it to return to the original styling when pressed again.



      Instead of changing the style of the actual svg elements, I am thinking that it might be easier to just change the style of the class:



      .leaflet-div-icon {
      background: rgba(0,0,0,0);
      border: none;
      }


      Where I then want to have a border when the cluster/marker has been pressed. I do not know, whether it is possible to change the class within the on clusterclick or click functions, or if it can be done in another way.



      My code, as it is now can be found here - where the wanted effect also can be seen on the controls on the right side: http://bl.ocks.org/skov94/f006cd45d2daa2bc67e4f514774fdd0d







      css d3.js leaflet






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 11:57









      LindaLinda

      13210




      13210
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Instead of switching the outline property of the leaflet-interactive div, i would toggle a class as you did with the controls on the right side (say a outlined class).



          This class toggling has to be done in a "onclick" event handler. Leaflet clustering provide its own cluster click events (clusterclick).



          The possible targets of the clusterclick event seem to be either the text, circle, or svg nodes of the cluster. We want to get the enclosing div with class leaflet-interactive to add or remove the outlined class on it. This will be made easily possible with Element.closest:



          Javascript file



          markers
          [...]
          .on('clusterclick',function(c) {

          console.log("pressed");
          map.closePopup();

          c.originalEvent.target.closest(".leaflet-interactive")
          .classList.toggle("outlined");

          });


          Then, simply change the style of its circle descendants with css:



          CSS file



          .leaflet-interactive.outlined circle {
          stroke-width: 2px;
          stroke: blue;
          }


          Edit: If you're not familiar with css, the selector means: circle nodes that are descendants of nodes with classes leaflet-interactive AND outlined.






          share|improve this answer


























          • But my problem is that I do not understand how to toggle between classes, when the class is stated in L.divIcon :)

            – Linda
            Nov 21 '18 at 16:48













          • You can simply add a click event handler afterwards. I've edited my answer to show how to do it.

            – Zim
            Nov 21 '18 at 16:56













          • Okay, it seems that i never enter the on click function, so let me just see if I fully understand: In my css file I create .leaflet-interactive and .leaflet-interactive.outlined circle. In the defineClusterIcon function I return a L.divIcon with the class 'leaflet-interactive' and outside the function, I then do the d3.selectAll(... ?

            – Linda
            Nov 21 '18 at 17:10











          • I've edited with extra information. Hope it will be clearer :)

            – Zim
            Nov 21 '18 at 17:55











          • It seems very clear, but I just can't get it to work - I would be extremely grateful, if maybe you could tell me what I am doing wrong; bl.ocks.org/Skov94/f006cd45d2daa2bc67e4f514774fdd0d

            – Linda
            Nov 21 '18 at 20:17











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411559%2fchanging-the-style-of-a-leaflet-cluster-when-pressing-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Instead of switching the outline property of the leaflet-interactive div, i would toggle a class as you did with the controls on the right side (say a outlined class).



          This class toggling has to be done in a "onclick" event handler. Leaflet clustering provide its own cluster click events (clusterclick).



          The possible targets of the clusterclick event seem to be either the text, circle, or svg nodes of the cluster. We want to get the enclosing div with class leaflet-interactive to add or remove the outlined class on it. This will be made easily possible with Element.closest:



          Javascript file



          markers
          [...]
          .on('clusterclick',function(c) {

          console.log("pressed");
          map.closePopup();

          c.originalEvent.target.closest(".leaflet-interactive")
          .classList.toggle("outlined");

          });


          Then, simply change the style of its circle descendants with css:



          CSS file



          .leaflet-interactive.outlined circle {
          stroke-width: 2px;
          stroke: blue;
          }


          Edit: If you're not familiar with css, the selector means: circle nodes that are descendants of nodes with classes leaflet-interactive AND outlined.






          share|improve this answer


























          • But my problem is that I do not understand how to toggle between classes, when the class is stated in L.divIcon :)

            – Linda
            Nov 21 '18 at 16:48













          • You can simply add a click event handler afterwards. I've edited my answer to show how to do it.

            – Zim
            Nov 21 '18 at 16:56













          • Okay, it seems that i never enter the on click function, so let me just see if I fully understand: In my css file I create .leaflet-interactive and .leaflet-interactive.outlined circle. In the defineClusterIcon function I return a L.divIcon with the class 'leaflet-interactive' and outside the function, I then do the d3.selectAll(... ?

            – Linda
            Nov 21 '18 at 17:10











          • I've edited with extra information. Hope it will be clearer :)

            – Zim
            Nov 21 '18 at 17:55











          • It seems very clear, but I just can't get it to work - I would be extremely grateful, if maybe you could tell me what I am doing wrong; bl.ocks.org/Skov94/f006cd45d2daa2bc67e4f514774fdd0d

            – Linda
            Nov 21 '18 at 20:17
















          1














          Instead of switching the outline property of the leaflet-interactive div, i would toggle a class as you did with the controls on the right side (say a outlined class).



          This class toggling has to be done in a "onclick" event handler. Leaflet clustering provide its own cluster click events (clusterclick).



          The possible targets of the clusterclick event seem to be either the text, circle, or svg nodes of the cluster. We want to get the enclosing div with class leaflet-interactive to add or remove the outlined class on it. This will be made easily possible with Element.closest:



          Javascript file



          markers
          [...]
          .on('clusterclick',function(c) {

          console.log("pressed");
          map.closePopup();

          c.originalEvent.target.closest(".leaflet-interactive")
          .classList.toggle("outlined");

          });


          Then, simply change the style of its circle descendants with css:



          CSS file



          .leaflet-interactive.outlined circle {
          stroke-width: 2px;
          stroke: blue;
          }


          Edit: If you're not familiar with css, the selector means: circle nodes that are descendants of nodes with classes leaflet-interactive AND outlined.






          share|improve this answer


























          • But my problem is that I do not understand how to toggle between classes, when the class is stated in L.divIcon :)

            – Linda
            Nov 21 '18 at 16:48













          • You can simply add a click event handler afterwards. I've edited my answer to show how to do it.

            – Zim
            Nov 21 '18 at 16:56













          • Okay, it seems that i never enter the on click function, so let me just see if I fully understand: In my css file I create .leaflet-interactive and .leaflet-interactive.outlined circle. In the defineClusterIcon function I return a L.divIcon with the class 'leaflet-interactive' and outside the function, I then do the d3.selectAll(... ?

            – Linda
            Nov 21 '18 at 17:10











          • I've edited with extra information. Hope it will be clearer :)

            – Zim
            Nov 21 '18 at 17:55











          • It seems very clear, but I just can't get it to work - I would be extremely grateful, if maybe you could tell me what I am doing wrong; bl.ocks.org/Skov94/f006cd45d2daa2bc67e4f514774fdd0d

            – Linda
            Nov 21 '18 at 20:17














          1












          1








          1







          Instead of switching the outline property of the leaflet-interactive div, i would toggle a class as you did with the controls on the right side (say a outlined class).



          This class toggling has to be done in a "onclick" event handler. Leaflet clustering provide its own cluster click events (clusterclick).



          The possible targets of the clusterclick event seem to be either the text, circle, or svg nodes of the cluster. We want to get the enclosing div with class leaflet-interactive to add or remove the outlined class on it. This will be made easily possible with Element.closest:



          Javascript file



          markers
          [...]
          .on('clusterclick',function(c) {

          console.log("pressed");
          map.closePopup();

          c.originalEvent.target.closest(".leaflet-interactive")
          .classList.toggle("outlined");

          });


          Then, simply change the style of its circle descendants with css:



          CSS file



          .leaflet-interactive.outlined circle {
          stroke-width: 2px;
          stroke: blue;
          }


          Edit: If you're not familiar with css, the selector means: circle nodes that are descendants of nodes with classes leaflet-interactive AND outlined.






          share|improve this answer















          Instead of switching the outline property of the leaflet-interactive div, i would toggle a class as you did with the controls on the right side (say a outlined class).



          This class toggling has to be done in a "onclick" event handler. Leaflet clustering provide its own cluster click events (clusterclick).



          The possible targets of the clusterclick event seem to be either the text, circle, or svg nodes of the cluster. We want to get the enclosing div with class leaflet-interactive to add or remove the outlined class on it. This will be made easily possible with Element.closest:



          Javascript file



          markers
          [...]
          .on('clusterclick',function(c) {

          console.log("pressed");
          map.closePopup();

          c.originalEvent.target.closest(".leaflet-interactive")
          .classList.toggle("outlined");

          });


          Then, simply change the style of its circle descendants with css:



          CSS file



          .leaflet-interactive.outlined circle {
          stroke-width: 2px;
          stroke: blue;
          }


          Edit: If you're not familiar with css, the selector means: circle nodes that are descendants of nodes with classes leaflet-interactive AND outlined.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 21 '18 at 20:36

























          answered Nov 21 '18 at 16:08









          ZimZim

          1,0741817




          1,0741817













          • But my problem is that I do not understand how to toggle between classes, when the class is stated in L.divIcon :)

            – Linda
            Nov 21 '18 at 16:48













          • You can simply add a click event handler afterwards. I've edited my answer to show how to do it.

            – Zim
            Nov 21 '18 at 16:56













          • Okay, it seems that i never enter the on click function, so let me just see if I fully understand: In my css file I create .leaflet-interactive and .leaflet-interactive.outlined circle. In the defineClusterIcon function I return a L.divIcon with the class 'leaflet-interactive' and outside the function, I then do the d3.selectAll(... ?

            – Linda
            Nov 21 '18 at 17:10











          • I've edited with extra information. Hope it will be clearer :)

            – Zim
            Nov 21 '18 at 17:55











          • It seems very clear, but I just can't get it to work - I would be extremely grateful, if maybe you could tell me what I am doing wrong; bl.ocks.org/Skov94/f006cd45d2daa2bc67e4f514774fdd0d

            – Linda
            Nov 21 '18 at 20:17



















          • But my problem is that I do not understand how to toggle between classes, when the class is stated in L.divIcon :)

            – Linda
            Nov 21 '18 at 16:48













          • You can simply add a click event handler afterwards. I've edited my answer to show how to do it.

            – Zim
            Nov 21 '18 at 16:56













          • Okay, it seems that i never enter the on click function, so let me just see if I fully understand: In my css file I create .leaflet-interactive and .leaflet-interactive.outlined circle. In the defineClusterIcon function I return a L.divIcon with the class 'leaflet-interactive' and outside the function, I then do the d3.selectAll(... ?

            – Linda
            Nov 21 '18 at 17:10











          • I've edited with extra information. Hope it will be clearer :)

            – Zim
            Nov 21 '18 at 17:55











          • It seems very clear, but I just can't get it to work - I would be extremely grateful, if maybe you could tell me what I am doing wrong; bl.ocks.org/Skov94/f006cd45d2daa2bc67e4f514774fdd0d

            – Linda
            Nov 21 '18 at 20:17

















          But my problem is that I do not understand how to toggle between classes, when the class is stated in L.divIcon :)

          – Linda
          Nov 21 '18 at 16:48







          But my problem is that I do not understand how to toggle between classes, when the class is stated in L.divIcon :)

          – Linda
          Nov 21 '18 at 16:48















          You can simply add a click event handler afterwards. I've edited my answer to show how to do it.

          – Zim
          Nov 21 '18 at 16:56







          You can simply add a click event handler afterwards. I've edited my answer to show how to do it.

          – Zim
          Nov 21 '18 at 16:56















          Okay, it seems that i never enter the on click function, so let me just see if I fully understand: In my css file I create .leaflet-interactive and .leaflet-interactive.outlined circle. In the defineClusterIcon function I return a L.divIcon with the class 'leaflet-interactive' and outside the function, I then do the d3.selectAll(... ?

          – Linda
          Nov 21 '18 at 17:10





          Okay, it seems that i never enter the on click function, so let me just see if I fully understand: In my css file I create .leaflet-interactive and .leaflet-interactive.outlined circle. In the defineClusterIcon function I return a L.divIcon with the class 'leaflet-interactive' and outside the function, I then do the d3.selectAll(... ?

          – Linda
          Nov 21 '18 at 17:10













          I've edited with extra information. Hope it will be clearer :)

          – Zim
          Nov 21 '18 at 17:55





          I've edited with extra information. Hope it will be clearer :)

          – Zim
          Nov 21 '18 at 17:55













          It seems very clear, but I just can't get it to work - I would be extremely grateful, if maybe you could tell me what I am doing wrong; bl.ocks.org/Skov94/f006cd45d2daa2bc67e4f514774fdd0d

          – Linda
          Nov 21 '18 at 20:17





          It seems very clear, but I just can't get it to work - I would be extremely grateful, if maybe you could tell me what I am doing wrong; bl.ocks.org/Skov94/f006cd45d2daa2bc67e4f514774fdd0d

          – Linda
          Nov 21 '18 at 20:17




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411559%2fchanging-the-style-of-a-leaflet-cluster-when-pressing-it%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?