JS API模块之FeatureLayer要素点击查询
2022/7/24 6:25:15
本文主要是介绍JS API模块之FeatureLayer要素点击查询,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
一、PopupTemplate实现
var featureLayer = new FeatureLayer({ url: "...", }); map.add(featureLayer); featureLayer.when(() => { let popupTemplate = { title: featureLayer.title, outFields: ["*"], content: [{ type: "fields", fieldInfos: featureLayer.fields.map(item=>({fieldName:item.name})) }] }; // console.log(popupTemplate); featureLayer.popupTemplate = popupTemplate; })
实现效果
二、View实现
var featureLayer = new FeatureLayer({ url: "...", }); // 创建一个GraphicLayer,用来绘制查询结果 var graphicsLayer = new GraphicsLayer(); map.add(graphicsLayer); map.add(featureLayer); view.on("click", (event)=> { view.hitTest(event).then(function (results) { console.log("results:",results); if (results.results.length) { addGraphics(results.results); } }); }); function addGraphics(result) { graphicsLayer.removeAll(); result.forEach(function (item) { if (item.graphic.geometry.type == "point") { } else if (item.graphic.geometry.type == "polyline") { } else if (item.graphic.geometry.type == "polygon") { const fillSymbol = { type: "simple-fill", color: [227, 139, 79, 0.8], outline: { color: [255, 255, 255], width: 1, }, }; var g = new Graphic({ geometry: item.graphic.geometry, symbol: fillSymbol, spatialReference: map.spatialReference }); } // console.log(Object.prototype.toString.call(feature)); graphicsLayer.add(g); view.goTo(g); }); }
实现效果
三、FeatureLayer实现
var featureLayer = new FeatureLayer({ url: "...", }); // 创建一个GraphicLayer,用来绘制查询结果 var graphicsLayer = new GraphicsLayer(); map.add(graphicsLayer); map.add(featureLayer); // Set up a click event handler and retrieve the screen point view.on("click", (event) => { // 查询的是attributes的内容 let query = featureLayer.createQuery(); query.geometry = event.mapPoint; query.spatialRelationship = "intersects"; featureLayer.queryFeatures(query).then(function (results) { // 返回一个要素集合 console.log("results:", results); if(results.features.length>0) addGraphics(results.features); }) }) function addGraphics(result) { graphicsLayer.removeAll(); result.forEach(function (feature) { if (feature.geometry.type == "point") { } else if (feature.geometry.type == "polyline") { } else if (feature.geometry.type == "polygon") { const fillSymbol = { type: "simple-fill", color: [227, 139, 79, 0.8], outline: { color: [255, 255, 255], width: 1, }, }; var g = new Graphic({ geometry: feature.geometry, symbol: fillSymbol, spatialReference: map.spatialReference }); } // console.log(Object.prototype.toString.call(feature)); graphicsLayer.add(g); view.goTo(g); }); }
实现效果
四、FeatureLayerView实现
var featureLayer = new FeatureLayer({ url: "...", }); let highlight; view.on("click", (event) => { view.whenLayerView(featureLayer).then(function (layerView) { // 查询的是attributes的内容 let query = layerView.createQuery(); query.geometry = event.mapPoint; query.spatialRelationship = "intersects"; layerView.queryFeatures(query).then(function (result) { console.log("result:", result); if (highlight) { highlight.remove(); } if (result.features.length) { highlight = layerView.highlight(result.features); view.goTo(result.features[0].geometry); } }) }); })
实现效果
我们可以看到FeatureLayer和FeatureLayerView查询的返回结果几乎一致,那区别在哪里呢?
FeatureLayer
FeatureLayerView
可以看出,FeatureLayer
返回的attributes是属性表所有字段及内容,而FeatureLayerView
返回的attributes只含有部分属性表的字段及内容。
所以在使用query.where
指定属性查询的时候,就有所区别了。
view
这篇关于JS API模块之FeatureLayer要素点击查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Vue新手入门教程:从零开始学习Vue框架
- 2024-11-23如何集成Ant Design Vue的图标
- 2024-11-23如何集成Ant Design Vue图标
- 2024-11-23使用vue CLI快速搭建Vue项目教程
- 2024-11-23Vue CLI多环境配置简单教程
- 2024-11-23Vue3入门教程:轻松搭建你的第一个Vue3应用
- 2024-11-23Vue3+Vite快速上手指南
- 2024-11-23Vue3阿里系UI组件入门指南
- 2024-11-23Vue3的阿里系UI组件入门指南
- 2024-11-23Vue3公共组件入门教程