如何获取arcgis server发布的FeatureLayer图层中各要素,并根据名称着重标出该要素?
作者:互联网
需求:已知根据数据后台查询到要素名称,想通过该名称查询要素多边形,不通过arcgis for jsp 提供的查询按钮实现。
这里只是一个demo,具体实施得结合自己的项目。
首先引入引用文件:
<link rel="stylesheet" href="https://js.arcgis.com/3.29/dijit/themes/tundra/tundra.css">
<link rel="stylesheet" href="https://js.arcgis.com/3.29/esri/css/esri.css">
<script src="https://js.arcgis.com/3.29/"></script>
接下来是添加arcgis api的引用函数及地图显示设置
var map, dialog;
require([
"esri/map", "esri/layers/FeatureLayer",
"esri/symbols/SimpleFillSymbol", "esri/symbols/SimpleLineSymbol",
"esri/renderers/SimpleRenderer", "esri/graphic", "esri/lang",
"esri/Color", "dojo/number", "dojo/dom-style",
"dijit/TooltipDialog", "dijit/popup", "dojo/domReady!"
], function(
Map, FeatureLayer,
SimpleFillSymbol, SimpleLineSymbol,
SimpleRenderer, Graphic, esriLang,
Color, number, domStyle,
TooltipDialog, dijitPopup
) {
map = new Map("mapDiv", {
basemap: "streets",
center: [-80.94, 33.646],
zoom: 8,
slider: false
});
然后添加要素图层,即FeatureLayer图层,这里面outFields用于过滤哪些字段,symbol为显示样式
var southCarolinaCounties = new FeatureLayer("https://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3", {
mode: FeatureLayer.MODE_SNAPSHOT,
outFields: ["NAME", "POP2000", "POP2007", "POP00_SQMI", "POP07_SQMI"]
});
southCarolinaCounties.setDefinitionExpression("STATE_NAME = 'South Carolina'");
var symbol = new SimpleFillSymbol(
SimpleFillSymbol.STYLE_SOLID,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([255,255,255,0.35]),
1
),
new Color([125,125,125,0.35])
);
southCarolinaCounties.setRenderer(new SimpleRenderer(symbol));
map.addLayer(southCarolinaCounties);
然后设定一个被选中要素的显示样式,即高亮显示的样式
var highlightSymbol = new SimpleFillSymbol(
SimpleFillSymbol.STYLE_SOLID,
new SimpleLineSymbol(
SimpleLineSymbol.STYLE_SOLID,
new Color([255,0,0]), 3
),
new Color([125,125,125,0.35])
);
接下面便是核心,即如何根据要素块的名称获取要素,并将该要素高亮显示,c表示整个FeatureLayer中地理要素,其中包括属性及空间位置,插件内部定义的
map.on("load", function(){
//我们需要用到的就是下面的代码
var handle = southCarolinaCounties.on("update-end", function(){
var c = southCarolinaCounties.graphics;
//打印所有的要素块
console.log(c);
//定义需要找到的块名称
var name = "Lee";
//遍历所有的要素,找到该要素,在该要素位置新建一个高亮图层
for(i=0;i<=c.length;i++){
if(c[i].attributes.NAME == name){
var highlightGraphic = new Graphic(c[i].geometry,highlightSymbol);
map.graphics.add(highlightGraphic);
}
}
//每次地图发生变换都会执行update-end事件,handle.remove()是为了避免代码被重复执行
handle.remove();
});
});
效果如下:
当name =“Lee”
当name = “Cherokee”
完整代码如下:
标签:要素,FeatureLayer,arcgis,125,var,new,esri 来源: https://blog.csdn.net/weixin_42464154/article/details/96133214