Esri Leaflet

Loading a Feature Layer programatically

Sometimes it is desirable to load a "snapshot" of a Feature Layer instead of loading it progressivly as you pan around the map. The approch is useful for smaller services.

<html>
<head>
  <meta charset=utf-8 />
  <title>Loading a Feature Layer programatically</title>
  <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />

  <!-- Load Leaflet from CDN-->
  <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
  <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>

  <!-- Load Esri Leaflet from CDN -->
  <script src="http://cdn-geoweb.s3.amazonaws.com/esri-leaflet/1.0.0-rc.2/esri-leaflet.js"></script>

  <style>
    body {margin:0;padding:0;}
    #map {position: absolute;top:0;bottom:0;right:0;left:0;}
  </style>
</head>
<body>

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

<script>
  var map = new L.Map("map").setView([45.525231693565054, -122.62836456298828], 11);

  L.esri.basemapLayer("Topographic").addTo(map);

  var neighborhoodsUrl = "http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Neighborhoods_pdx/FeatureServer/0";
  var bikeParkingUrl = "http://services.arcgis.com/rOo16HdIMeOBI4Mb/arcgis/rest/services/Portland_Bicycle_Parking/FeatureServer/0";

  // query a feature service for a neighborhood
  L.esri.Tasks.query(neighborhoodsUrl).where("NAME='HOLLYWOOD'").run(function(error, neighborhoods){
    // draw neighborhood on the map
    var hollywood = L.geoJson(neighborhoods).addTo(map);

    // fit map to boundry
    map.fitBounds(hollywood.getBounds().pad(0.5));

    // query all bike parking where BPSTYLE='STAPLE' inside our boundry
    L.esri.Tasks.query(bikeParkingUrl).where("BPSTYLE='STAPLE'").within(hollywood).run(function(error, bikeParking){
      L.geoJson(bikeParking).addTo(map);
    });
  });
</script>

</body>
</html>