在arcgis api for js中,使用sketch绘制多边形,判断featureLayer中的矩形要素是否在多边形内部,若在内部则高亮显示该矩形要素
时间: 2024-04-06 15:29:26 浏览: 150
好的,以下是使用ArcGIS API for JavaScript实现该功能的示例代码:
```javascript
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/widgets/Sketch",
"esri/geometry/geometryEngine",
"esri/Graphic"
], function(Map, MapView, FeatureLayer, Sketch, geometryEngine, Graphic) {
var map = new Map({
basemap: "streets-navigation-vector"
});
var view = new MapView({
container: "viewDiv",
map: map,
center: [-118.805, 34.027],
zoom: 13
});
var featureLayer = new FeatureLayer({
url: "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Los_Angeles_County_Parcels/FeatureServer/0"
});
map.add(featureLayer);
var sketch = new Sketch({
view: view,
layer: featureLayer,
creationMode: "update"
});
view.ui.add(sketch, "top-right");
sketch.on("update", function(event) {
if (event.state === "complete") {
var polygon = event.graphic.geometry;
featureLayer.queryFeatures().then(function(results) {
results.features.forEach(function(feature) {
var geometry = feature.geometry;
if (geometry.type === "polygon" && geometryEngine.contains(polygon, geometry)) {
var highlightSymbol = {
type: "simple-fill",
color: [255, 0, 0, 0.5],
style: "solid",
outline: {
color: [255, 0, 0, 1],
width: 2
}
};
var highlightGraphic = new Graphic({
geometry: geometry,
symbol: highlightSymbol
});
view.graphics.add(highlightGraphic);
}
});
});
}
});
});
```
在上述代码中,我们首先创建了一个地图和地图视图对象,并添加了一个要素图层。接着,我们创建了一个Sketch工具,并将其添加到视图中。当用户使用Sketch工具绘制完多边形后,我们会监听其`update`事件,并在事件处理函数中查询要素图层中的所有要素,然后遍历每个要素,判断其是否为多边形类型并且是否被绘制的多边形包含。如果是,则创建一个高亮显示的图形,并将其添加到视图的graphics图层中。在上述代码中,我们使用了ArcGIS API for JavaScript提供的`geometryEngine`模块的`contains`方法来判断是否包含。最后,我们还需要设置高亮显示图形的样式,这里使用了一个简单的红色半透明填充样式。
阅读全文