Viewing an older version of MapBox.js. Check out v1.5.0 for the latest.

Simple filtering

Show all markers or just those matching a filter function
Select all
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Simple filtering</title>
  
  <script src='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.js'></script>
  <link href='http://api.tiles.mapbox.com/mapbox.js/v0.6.7/mapbox.css' rel='stylesheet' />
  
  <style>
    body { margin:0; padding:0; }
    #map { position:absolute; top:0; bottom:0; width:100%; }
  </style>
</head>
<body>
<style>
 #map-ui {
    position:absolute;
    top:10px;left:10px;
    z-index:100;
    }
    #map-ui ul {
      list-style:none;
      margin:0;padding:0;
      }
      #map-ui a {
        font-size:13px;
        background:#FFF;
        color:#3C4E5A;
        display:block;
        margin:0;padding:0;
        border:1px solid #BBB;
        border-bottom-width:0;
        min-width:138px;
        padding:10px;
        text-decoration:none;
        }
      #map-ui a:hover { background:#ECF5FA; }
      #map-ui li:last-child a {
        border-bottom-width:1px;
        -webkit-border-radius:0 0 3px 3px;
                border-radius:0 0 3px 3px;
        }
      #map-ui li:first-child a {
        -webkit-border-radius:3px 3px 0 0;
                border-radius:3px 3px 0 0;
        }
      #map-ui a.active {
        background:#3887BE;
        border-color:#3887BE;
        color:#FFF;
        }
</style>
<div id='map-ui'>
  <ul>
    <li><a href='#' id='filter-food'>show only food events</a></li>
    <li><a href='#' class='active' id='filter-all'>show all events</a></li>
  </ul>
</div>
<div id='map'></div>
<script>
  var map = mapbox.map('map');

  map.addLayer(mapbox.layer().id('examples.map-zr0njcqy'));

  var markersLayer = mapbox.markers.layer().id('examples.map-zr0njcqy', function() {

      // This code is in the second callback to id because it depends
      // on the features being loaded asynchronously. If it were put right
      // after the call to .id(), the markers layer wouldn't contain any
      // data - it would still be on its way from MapBox to the browser.

      // Cinch the map display to show all markers
      map.extent(markersLayer.extent());

      var food = document.getElementById('filter-food');
      var all = document.getElementById('filter-all');

      food.onclick = function(e) {
          all.className = '';
          this.className = 'active';
          // The filter function takes a GeoJSON feature object
          // and returns true to show it or false to hide it.
          markersLayer.filter(function(f) {
              return f.properties['marker-symbol'] === 'fast-food';
          });
          return false;
      };

      all.onclick = function() {
          food.className = '';
          this.className = 'active';
          markersLayer.filter(function(f) {
              // Returning true for all markers shows everything.
              return true;
          });
          return false;
      };

  });

  map.addLayer(markersLayer);

  // Attribute map
  map.ui.attribution.add()
      .content('<a href="http://mapbox.com/about/maps">Terms &amp; Feedback</a>');
</script>
The code and documentation to mapbox.js is hosted on GitHub where you can contribute changes and improvements.