Esri Leaflet
This content has moved to developers.arcgis.com. Please update your bookmarks!

Spatial Queries on a Feature Layer

Create detailed spatial queries like point within polygon or line intersects polygons with Feature Layer queries. Some combinations will not result in any features being highlighted.

Neighborhoods
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>Spatial Queries on a Feature Layer</title>
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />

  <!-- Load Leaflet from CDN -->
  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
    integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
    crossorigin=""/>
  <script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
    integrity="sha512-XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
    crossorigin=""></script>

  <!-- Load Esri Leaflet from CDN -->
  <script src="https://unpkg.com/esri-leaflet@3.0.8/dist/esri-leaflet.js"
    integrity="sha512-E0DKVahIg0p1UHR2Kf9NX7x7TUewJb30mxkxEm2qOYTVJObgsAGpEol9F6iK6oefCbkJiA4/i6fnTHzM6H1kEA=="
    crossorigin=""></script>

  <!-- Load Esri Leaflet Vector from CDN -->
  <script src="https://unpkg.com/esri-leaflet-vector@4.0.0/dist/esri-leaflet-vector.js"
    integrity="sha512-EMt/tpooNkBOxxQy2SOE1HgzWbg9u1gI6mT23Wl0eBWTwN9nuaPtLAaX9irNocMrHf0XhRzT8B0vXQ/bzD0I0w=="
    crossorigin=""></script>

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

<style>
  #panel {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1000;
    background: white;
    padding: 10px;
  }
</style>

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

<div id="panel" class="leaflet-bar">
  Neighborhoods
  <select name="relation" id="relationSelect">
    <option value="within">Within<options>
    <option value="contains">Contains<options>
    <option value="intersects">Intersects<options>
    <option value="overlaps">Overlaps<options>
  </select>
  <select name="geometry" id="geometrySelect">
    <option value="bounds">Bounds<options>
    <option value="point">Point<options>
    <option value="line">Line<options>
    <option value="polygon">Polygon<options>
  </select>
  <button id="executeQuery">Run Query</button>
</div>

<script>
  var map = L.map('map').setView([45.525, -122.628], 11);

  L.esri.Vector.vectorBasemapLayer('ArcGIS:Topographic', {
    apikey: apiKey // Replace with your API key - https://developers.arcgis.com
  }).addTo(map);

  // create our layer
  var neighborhoods = L.esri.featureLayer({
    url: 'https://www.portlandmaps.com/arcgis/rest/services/Public/Zoning/MapServer/32',
    style: {
      color: '#000',
      weight: 1,
      opacity: 0.4
    }
  }).addTo(map);

  // create a marker object to query against
  var marker = L.marker([45.571034, -122.686386]).addTo(map);

  // create a bounds object to query against
  var bounds = L.latLngBounds([
    [45.494556, -122.691536],
    [45.538100, -122.608452]
  ]);

  // create a rectangle to visualize the bounds
  L.rectangle(bounds, {
    color: 'blue',
    weight: 2
  }).addTo(map);

  // create a line to query against
  var line = L.polyline([
    [45.559256, -122.611885],
    [45.502256, -122.562790],
    [45.483244, -122.620468]
  ], {
    color: 'blue',
    weight: 2
  }).addTo(map);

  // create a polygon to query against
  var polygon = L.polygon([
    [45.484894, -122.493696],
    [45.512025, -122.492199],
    [45.517669, -122.561457],
    [45.487343, -122.558573]
  ], {
    color: 'blue',
    weight: 2
  }).addTo(map);

  // collect geometries into an object so we can reference them later
  var geometries = {
    bounds: bounds,
    line: line,
    polygon: polygon,
    point: marker
  };

  // get references to our <select> elements
  var relationship = document.getElementById('relationSelect');
  var geometry = document.getElementById('geometrySelect');
  var runQueryButton = document.getElementById('executeQuery');

  var previousIds = [];

  // reset all features back to their regularly defined styles
  function reset () {
    for (var i = previousIds.length - 1; i >= 0; i--) {
      neighborhoods.resetStyle(previousIds[i]);
    }
}
  // query the API and highlight features
  function query () {
    reset();
    // lookup our input geometry
    var inputGeometry = geometries[geometry.value];

    // query the service executing the selected relation with the selected input geometry
    neighborhoods.query()[relationship.value](inputGeometry).ids(function (error, ids) {
      // if there is an error with the query, you can handle it here
      if (error) {
        console.log('Error with query: ' + error);
      } else if (ids) {
        previousIds = ids;
        for (var i = ids.length - 1; i >= 0; i--) {
          neighborhoods.setFeatureStyle(ids[i], { color: 'red', weight: 2 });
        }
      }
    });
  }

  // query when "Run Query" button is clicked
  runQueryButton.addEventListener('click', query);

  // once all neighborhoods have loaded run the default query
  neighborhoods.once('load', function () {
    query();
  });
</script>

</body>
</html>