Last active
April 26, 2019 21:49
-
-
Save nommuna2/f3e3224947b707e1e3cccebc54f58461 to your computer and use it in GitHub Desktop.
(ArcGIS API for JavaScript) Sample of selecting the nearest street using buffer in 3.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require([ | |
"esri/map", | |
"esri/layers/FeatureLayer", | |
"esri/renderers/SimpleRenderer", | |
"esri/symbols/SimpleFillSymbol", | |
"esri/symbols/SimpleLineSymbol", | |
"esri/Color", | |
"esri/tasks/query", | |
"esri/geometry/geometryEngine", | |
"esri/graphic", | |
"esri/layers/GraphicsLayer", | |
"dojo/domReady!" | |
], | |
function (Map, FeatureLayer, SimpleRenderer, SimpleFillSymbol, SimpleLineSymbol, Color, Query, geometryEngine, Graphic, GraphicsLayer) { | |
var map = new Map("map", { | |
basemap: "dark-gray", | |
center: [-77.0369, 38.9072], | |
zoom: 16 | |
}); | |
var mySymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, | |
new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, | |
new Color([255, 0, 0]), 2), new Color([255, 255, 0, 0.25]) | |
); | |
var lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color("blue")); | |
lineSymbol.setWidth(1); | |
var lineRenderer = new SimpleRenderer(lineSymbol); | |
var polylineLayer = new FeatureLayer("https://maps2.dcgis.dc.gov/dcgis/rest/services/DPW/SA_2018_streets_alleys/FeatureServer/0"); | |
polylineLayer.setRenderer(lineRenderer); | |
var lineSymbol2 = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color("red")); | |
polylineLayer.setSelectionSymbol(lineSymbol2); | |
map.addLayer(polylineLayer); | |
map.on("click", function (e) { | |
console.log(e); | |
nearMe(e.mapPoint, 20); //on map click send the mapPoint and the miles to buffer | |
}); | |
function nearMe(geometry, distance) { | |
//Buffer based on the geometry and meters | |
var result = geometryEngine.buffer(geometry, distance, "meters"); | |
//Set up a Spatial filter Query | |
var query = new Query(); | |
query.returnGeometry = true; | |
query.geometry = result; | |
//Query the Polyline feature layer using the spatial filter above | |
polylineLayer.queryFeatures(query, (e) => { | |
//Set up an empty array to hold the objectID's that intersect the buffer. | |
query.objectIds = []; | |
//Loop through all the results and push the ID's to the empty array that intersect the buffer. | |
e.features.forEach((results) => { | |
query.objectIds.push(results.attributes.OBJECTID); | |
}); | |
//Pass in the modified query to selectFeatures and show the selected polyline. | |
polylineLayer.selectFeatures(query, polylineLayer.SELECTION_NEW); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment