arcgis api for javascript學習-使用FeatureLayer結合FeatureTable實現查詢並能根據查詢結果將檢視縮放至結果的範圍
本文主要實現的功能是:
輸入查詢的條件(這裡是模糊查詢)
點選查詢得到查詢結果,並在屬性中顯示屬性(FeatureTable)
主要的思路:
1. 定義FeatureLayer和FeatureTable,FeatureTable中有個屬性zoomToSelection:true可以設定是否根據表和地圖聯動.(點選表中屬性縮放至所選擇的要素)點選圖中的查詢到的結果,表中屬性被選中.
2. 點選按鈕獲取文字框中的查詢條件,並將查詢條件通過myFeatureLayer.setDefinitionExpression(sql)來篩選得到FeatureLayer的結果.
3. 通過FeatureLayer的點選事件實現點選地圖對應表內容被選中.
4. 遍歷篩選得到的FeatureLayer並獲得最大的範圍(union()方法),再通過Map.setExtent(方法縮放到檢視.
5. 通過render渲染查詢的FeatureLayer
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>FeatureTable - related records</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.34/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="https://js.arcgis.com/3.34/esri/css/esri.css">
<script src="https://js.arcgis.com/3.34/"></script>
<style>
html, body, #map
{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script>
var map;
require([
"esri/map",
"esri/layers/ArcGISDynamicMapServiceLayer",
"esri/layers/ArcGISTiledMapServiceLayer",
"esri/layers/FeatureLayer",
"esri/dijit/FeatureTable",
"dojo/on",
"dojo/dom",
"esri/tasks/query",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/SimpleLineSymbol",
"esri/renderers/SimpleRenderer",
"esri/geometry/Extent"
], function (
Map,ArcGISDynamicMapServiceLayer,ArcGISTiledMapServiceLayer,FeatureLayer,FeatureTable,on,dom,Query,SimpleFillSymbol,SimpleLineSymbol,SimpleRenderer,Extent
) {
map = new Map("map",{
logo: false,
slider: false,
zoom: 5
});
var layer = new ArcGISDynamicMapServiceLayer("http://localhost:6080/arcgis/rest/services/TestServer/mytile2/MapServer/");
map.addLayer(layer);
// 要素圖層
var myFeatureLayer = new FeatureLayer("http://localhost:6080/arcgis/rest/services/TestServer/mytile2/MapServer/1",{
mode: FeatureLayer.MODE_ONDEMAND,
outFields: ["*"],
visible: true,
id : "fLayer",
opacity: 0.7
});
// 要素表格
var myFeatureTable = new FeatureTable({
featureLayer: myFeatureLayer,
map: map,
editable: true,
syncSelection: true,
showRelatedRecords: true,
showAttachments: true,
// zoomToSelection: true,
// outfields 顯示要素屬性
outFields: ["name","Shape_Leng"]
},"myTable");
myFeatureTable.startup();
on(dom.byId("selectBtn"),"click",function (e) {
// myFeatureTable.startup();
//移除之前的查詢結果
map.removeLayer(myFeatureLayer);
var selectText = dom.byId("selectText").value;
var sql = "name like '%" + selectText + "%'"; // 注意sql語句格式,錯了就查詢不到
// console.log(sql);
myFeatureLayer.setDefinitionExpression(sql);
var selectionSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID,
new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,
new dojo.Color("blue"), 1),
new dojo.Color("blue"));
myFeatureLayer.setSelectionSymbol(selectionSymbol); // 設定要素圖層被選中標誌
// 監聽要素圖層點選事件,並傳送相應的引數給表格
// 當地圖中要素被點選選中時,與之相關的屬性記錄也會在要素表格中被選中
myFeatureLayer.on("click",function (evt) {
var idProperty = myFeatureLayer.objectIdField;
// console.log(idProperty);
var feature, featureId, query;
if (evt.graphic && evt.graphic.attributes && evt.graphic.attributes[idProperty]){
feature = evt.graphic; //獲取要素
//點選地圖上的要素高亮顯示
feature.setSymbol(selectionSymbol);
map.graphics.add(feature);
featureId = feature.attributes[idProperty]; //獲取要素id
//例項化查詢
query = new Query();
query.returnGeometry = true;
query.objectIds = [featureId];
query.where = "1=1";
myFeatureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW);
}
});
var selExtent = new Extent();
var query = new Query();
query.where='1=1';
query.returnGeometry = true;
query.outFields = ['name'];
myFeatureLayer.queryFeatures(query, function (featureSet) {
var features = featureSet.features;
selExtent = features[0].geometry.getExtent();
console.log("selExtent start-->",selExtent);
for (var i = 0, len = features.length; i < len; i++) {
console.log(i,features[i].geometry.getExtent());
selExtent = selExtent.union(features[i].geometry.getExtent());
}
map.setExtent(selExtent);
});
// map.setExtent(selExtent);//為啥這個地方不能訪問selExtent
//查詢結果進行渲染
var render = new SimpleRenderer(selectionSymbol);
myFeatureLayer.setRenderer(render);
map.addLayer(myFeatureLayer);
myFeatureTable.refresh();
});
});
</script>
</head>
<body>
<div id="map">
<input type="text" id="selectText" />
<input type="button" id="selectBtn" value="查詢" />
</div>
<div id="selectRe" style="z-index: 9999; background-color: pink; width: 500px; height: 200px; position: absolute; bottom: 0;left: 0;">
<div id="myTable"></div>
</div>
</body>
</html>
相關文章
- UPDATE查詢結果範圍內的資料
- 根據不同欄位,升降序排列查詢結果
- mysql查詢結果多列拼接查詢MySql
- 使用查詢結果更新表的方法
- MySQL 查詢結果取交集的實現方法MySql
- 使用ROWNUM將導致查詢結果集的固化
- navicat 匯出查詢結果
- mysql匯出查詢結果MySql
- MySQL 將查詢結果匯出到檔案MySql
- mysql根據查詢結果批量更新多條資料(插入或更新)MySql
- 【記錄】sqlserver列轉行查詢 並將查詢結果用逗號分隔開SQLServer
- MySQL查詢結果匯出方式總結MySql
- Mybatis 查詢語句結果集總結MyBatis
- SQL Server對組合查詢結果排序方法SQLServer排序
- Mybatis模糊查詢結果為空MyBatis
- ps aux查詢結果解釋UX
- oracle 查詢結果的各種格式Oracle
- xsl中,對xml文件查詢的結果再次查詢XML
- mysql中的多行查詢結果合併成一個MySql
- PLSQL Developer 複製查詢結果 卡頓SQLDeveloper
- ORDER對查詢結果進行排序排序
- oracle查詢結果外面新增引號Oracle
- Oracle查詢結果 儲存為XMLOracleXML
- SQL 把查詢結果當作"表"來使用SQL
- [ORACLE BUG]查詢結果錯誤--臨時表並行Oracle並行
- Oracle Database 12c查詢最佳化器的缺陷-檢視合併會造成查詢結果不準確OracleDatabase
- UTL_FILE包,將查詢結果寫入檔案
- [Mysql 查詢語句]——對查詢結果進一步的操作MySql
- 等於NULL的查詢條件導致查詢結果不正確Null
- 備忘:laravel 對查詢結果集可以迴圈where查詢Laravel
- ES查詢之查詢屬性過濾、結果高亮顯示
- PostgreSQL函式:返回表查詢結果集SQL函式
- DBeaver 匯出多個查詢結果集
- 為 MySQL 的查詢結果新增排名欄位MySql
- 巧用臨時表將大結果集轉換為小結果集驅動查詢薦
- Mybatis底層原理學習(三):查詢結果集的處理原理MyBatis
- NC65對單表單據查詢的結果進行排序排序
- 根據時間範圍呼叫gitLab介面查詢Gitlab