Esri Leaflet

Loading a Feature Layer programatically

Sometimes it is desirable to load a "snapshot" of a Feature Layer instead of loading it progressively as you pan around the map. The approach 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="//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.css" />
  <script src="//cdn.jsdelivr.net/leaflet/0.7.3/leaflet.js"></script>

  <!-- Load Esri Leaflet from CDN -->
  <script src="//cdn.jsdelivr.net/leaflet.esri/1.0.3/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 = L.map("map").setView([45.525231693565054, -122.62836456298828], 11);

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

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

  // query a feature service for a neighborhood
  L.esri.Tasks.query({
    url: 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({
      url: bikeParkingUrl
    }).where("BPSTYLE='STAPLE'").within(hollywood).run(function(error, bikeParking){
      L.geoJson(bikeParking).addTo(map);
    });
  });
</script>

</body>
</html>