Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Something along these lines</p> <p>First, turn your layer into a variable</p> <pre><code>var mylayer = L.geoJson(statesData, {style: style}); map.addLayer(mylayer); </code></pre> <p>Create a new function for getting the new style based on your buttons id</p> <pre><code>function newStyle(id) { return { fillColor: getColor(id), weight: 2, opacity: 1, color: 'white', dashArray: '3', fillOpacity: 0.7 }; } </code></pre> <p>Then listen for the click and get the btn id and set the new style</p> <pre><code>$('body').on('click', 'btn', function (btn) { var id = $(btn).attr('id'); var new_style = newStyle(id); mylayer.setStyle(new_style); }); </code></pre> <p>UPDATE:</p> <p>Updated the getcolor to only be getColor(id). You should make your buttons correspond to the colors in the getColor function on the example. So id="11" will return #FED976 from the example, id="21" will return #FEB24C and so on. </p> <p>Alternatively you could just set your button ids to the color (id="#FED976") and then change the fillColor: getColor(id) to fillColor: id</p> <p>UPDATE2:</p> <pre><code>function style1(feature) { return { fillColor: getColor(feature.properties.btn1) }; } function style2(feature) { return { fillColor: getColor(feature.properties.btn2) }; } var mylayer = L.geoJson(statesData, {style: style}); map.addLayer(mylayer); $('body').on('click', 'btn', function (btn) { var id = $(btn).attr('id'); switch (id) { case "btn1": map.removeLayer(mylayer); mylayer = L.geoJson(statesData, {style: style1}); map.addLayer(mylayer); break; case "btn2": map.removeLayer(mylayer); mylayer = L.geoJson(statesData, {style: style2}); map.addLayer(mylayer); break; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload