You can perform detailed spatial queries on feature layer attributes.
<html>
<head>
<meta charset="utf-8" />
<title>Perform a spatial query</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
crossorigin=""/>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
crossorigin=""></script>
<!-- Load Esri Leaflet from CDN -->
<script src=https://unpkg.com/esri-leaflet@3.0.10/dist/esri-leaflet.js></script>
<!-- Load Esri Leaflet Vector from CDN -->
<script src="https://unpkg.com/esri-leaflet-vector@4.2.5/dist/esri-leaflet-vector.js" crossorigin=""></script>
<style>
html,
body,
#map {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #323232;
}
</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>
const accessToken = "YOUR_ACCESS_TOKEN";
const map = L.map('map').setView([45.525, -122.628], 11);
L.esri.Vector.vectorBasemapLayer('arcgis/outdoor', {
token: accessToken
}).addTo(map);
const 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);
const marker = L.marker([45.571034, -122.686386]).addTo(map); // Create a marker object to query against
const bounds = L.latLngBounds([ // Create a bounds object to query against
[45.494556, -122.691536],
[45.538100, -122.608452]
]);
L.rectangle(bounds, { // Create a rectangle to visualize the bounds
color: 'blue',
weight: 2
}).addTo(map);
const line = L.polyline([ // Create a line to query against
[45.559256, -122.611885],
[45.502256, -122.562790],
[45.483244, -122.620468]
], {
color: 'blue',
weight: 2
}).addTo(map);
const polygon = L.polygon([ // Create a polygon to query against
[45.484894, -122.493696],
[45.512025, -122.492199],
[45.517669, -122.561457],
[45.487343, -122.558573]
], {
color: 'blue',
weight: 2
}).addTo(map);
const geometries = {
bounds: bounds,
line: line,
polygon: polygon,
point: marker
};
const relationship = document.getElementById('relationSelect');
const geometry = document.getElementById('geometrySelect');
const runQueryButton = document.getElementById('executeQuery');
let previousIds = [];
function reset () { // Reset all features back to their regularly defined styles
for (let i = previousIds.length - 1; i >= 0; i--) {
neighborhoods.resetStyle(previousIds[i]);
}
}
function query () {
reset();
const inputGeometry = geometries[geometry.value];
neighborhoods.query()[relationship.value](inputGeometry).ids(function (error, ids) {
if (error) {
console.log('Error with query: ' + error);
} else if (ids) {
previousIds = ids;
for (let i = ids.length - 1; i >= 0; i--) {
neighborhoods.setFeatureStyle(ids[i], { color: 'red', weight: 2 });
}
}
});
}
runQueryButton.addEventListener('click', query);
neighborhoods.once('load', function () {
query();
});
</script>
</body>
</html>