filter geojson programmatically

<!DOCTYPE html>
<html>
<head>
    <title>GeoJSON Example</title>
    <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
    <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<style>
#wrapper {
  margin: 0 auto;
  width: 100%;
}

#panel{
 float: left;
  width: 100%;
  height:50px;
background-color: #175B81;
color:white;
}

#map {
  float: left;
  width: 80%;
  height:600px;
}

</style>
</head>
<body>

<div id="wrapper">
    <div id="panel"><span class="p2"></span>

            <select id="league" onchange="LeagueSelect()">
              <option value="NL" >National</option>
              <option value="AL" >American</option>
            </select>

    </div>
  <div id="map">  </div>

</div>
<script>
var url = 'BaseBallFinal.json';  

    var map = L.map('map').setView([39.0, -98.26], 4); 

    var osm=new L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',{ 
                attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'});
    osm.addTo(map);

    var myData =  L.layerGroup([]);

    var bbTeam = L.geoJSON(null, {
            //onEachFeature: forEachFeature, //not used
            pointToLayer: function(feature, latlng) {

                return L.circleMarker(latlng, {
                radius:6,
                opacity: .5,
                color:'green',
                fillColor:  'green',
                fillOpacity: 0.8

                }).bindTooltip(feature.properties.Name);
            }
      });

    // Get GeoJSON data and create features.

        $.getJSON(url, function(data) {
            bbTeam.addData(data);
        });

    myData.addLayer(bbTeam);
    myData.addTo(map); 

    // Now working with filtered data...

    function LeagueSelect() {
        var choice = document.getElementById("league").value;
        console.log(choice);

        var theColor;

        switch(choice) {
            case 'NL':
                theColor = 'blue'
                break;
            case 'AL':
                theColor = 'red'
                break;
            default:
                theColor = 'green'
        }

        myData.clearLayers();
        map.removeLayer(myData);

        bbTeam = L.geoJSON(null, {
            //onEachFeature: forEachFeature, //not used
            pointToLayer: function(feature, latlng) {

                return L.circleMarker(latlng, {
                radius:6,
                opacity: .5,
                color:theColor,
                fillColor: theColor,
                fillOpacity: 0.8

                }).bindTooltip(feature.properties.Name);
            },
            filter: function(feature, layer) {   
                 return (feature.properties.League == choice );
            },

      });

    // Get GeoJSON data and create features.

        $.getJSON(url, function(data) {
            bbTeam.addData(data);
        });

    myData.addLayer(bbTeam);
    myData.addTo(map); 
    }

 </script>
</body>
</html>

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source