ArcGIS API for JavaScript——獲取FeatureLayer的屬性值

出自點擊打開鏈接

當我們將一個圖層發佈爲服務後,在JS代碼中想取到圖層裏面的數據該怎麼做呢?在下面的例子中將演示當鼠標點擊圖層點時彈出圖層屬性的過程。 
一、測試數據 
這裏寫圖片描述 
二、發佈服務 
這裏寫圖片描述
三、引用服務 

引用服務的代碼如下:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Create Map and add a dynamic layer</title>
    <!--使用的是本機離線API-->
    <link rel="stylesheet" type="text/css" href="http://localhost/arcgis_js_api/library/3.18/3.18/dijit/themes/claro/claro.css"/>
    <link rel="stylesheet" type="text/css" href="http://localhost/arcgis_js_api/library/3.18/3.18/esri/css/esri.css" />
    <script src="http://localhost/arcgis_js_api/library/3.18/3.18/init.js" djConfig="parseOnLoad:true"></script>
    <style>
        html, body, #map {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
    </style>

    <script>
    require([
        "esri/map",
        "esri/layers/FeatureLayer",
        "esri/layers/LabelClass",
        "dojo/_base/Color",
        "esri/symbols/Font",
        "esri/symbols/TextSymbol",
        "dojo/domReady!"
      ],
      function(
        Map,
        FeatureLayer,
        LabelClass,Color,Font,TextSymbol
      ) {

        var map = new Map("map",{
            showLabels : true
        });

        /****************************************************************
         * Add feature layer - A FeatureLayer at minimum should point
         * to a URL to a feature service or point to a feature collection 
         * object.
         ***************************************************************/

        // Carbon storage of trees in Warren Wilson College.
        var featureLayer = new FeatureLayer("http://localhost:6080/arcgis/rest/services/cs/MapServer/0",{
            mode: FeatureLayer.MODE_SNAPSHOT,
           outFields: ["*"]  //顯示所有字段
        });

        //地名標註
        var labelSymbol = new TextSymbol().setColor(new Color("#000000"));
        labelSymbol.font.setSize("10pt");
        labelSymbol.font.setFamily("新宋體");
        var json = {
            "labelExpressionInfo": {"value": "{Point}"},
            "useCodedValues": false,
            "labelPlacement":"center-right"
        };
        var labelClass = new LabelClass(json);
        labelClass.symbol = labelSymbol;
        featureLayer.setLabelingInfo([ labelClass ]);
        map.addLayer(featureLayer);

      });
    </script>
    </head>

    <body>
      <div id="map"></div>
    </body>

</html>


運行結果: 
這裏寫圖片描述
四、輸出圖層屬性的值 
1、輸出featureLayer 

首先看看添加的featureLayer輸出的是什麼?

...
        map.addLayer(featureLayer);
        console.log(featureLayer);
...
打開瀏覽器的控制檯可以看到輸出的是一個Object對象,包含圖層的信息。 
這裏寫圖片描述
其中重點是graphics 
這裏寫圖片描述 
graphics是一個數組,展開graphics可以看到有5個變量,對應5個點的數據。 
這裏寫圖片描述
隨便展開一個,可以看到圖層的屬性數據就包含在attributes中。 
這裏寫圖片描述
2、輸出graphics 

在前面我們看到圖層的數據就存在graphics中,那把graphics輸出會得到什麼呢?

...
map.addLayer(featureLayer);
//輸出graphics
console.log(featureLayer.graphics);
...
運行結果: 
這裏寫圖片描述 
結果卻顯示graphics的長度爲0,裏面沒有數據。 
這是因爲layer還沒有添加到map中,所以拿不到屬性數據。 

3、添加“update-end”事件

...
map.addLayer(featureLayer);
//添加“update-end”事件
map.on("update-end", function(){           
// update-end event will execute after the layer has been added to the map
if(featureLayer.graphics.length >= 1)
{
       // do your thing
console.log(featureLayer.graphics);      
}
});
...
運行結果: 
這裏寫圖片描述

4、輸出attributes

...
//輸出第一條記錄的Point變量和NUM變量
var point=featureLayer.graphics[0].attributes.Point;
var num=featureLayer.graphics[0].attributes.NUM;

console.log("Point:"+point+"    NUM:"+num);
...
運行結果: 
這裏寫圖片描述

三、完整代碼

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Create Map and add a dynamic layer</title>
    <link rel="stylesheet" type="text/css" href="http://localhost/arcgis_js_api/library/3.18/3.18/dijit/themes/claro/claro.css"/>
    <link rel="stylesheet" type="text/css" href="http://localhost/arcgis_js_api/library/3.18/3.18/esri/css/esri.css" />
    <script src="http://localhost/arcgis_js_api/library/3.18/3.18/init.js" djConfig="parseOnLoad:true"></script>
    <style>
        html, body, #map {
          padding: 0;
          margin: 0;
          height: 100%;
          width: 100%;
        }
    </style>

    <script>
    require([
        "esri/map",
        "esri/layers/FeatureLayer",
        "esri/layers/LabelClass",
        "dojo/_base/Color",
        "esri/symbols/Font",
        "esri/symbols/TextSymbol",
        "dojo/domReady!"
      ],
      function(
        Map,
        FeatureLayer,
        LabelClass,Color,Font,TextSymbol
      ) {

        var map = new Map("map",{
            showLabels : true
        });

        /****************************************************************
         * Add feature layer - A FeatureLayer at minimum should point
         * to a URL to a feature service or point to a feature collection 
         * object.
         ***************************************************************/

        // Carbon storage of trees in Warren Wilson College.
        var featureLayer = new FeatureLayer("http://localhost:6080/arcgis/rest/services/cs/MapServer/0",{
            mode: FeatureLayer.MODE_SNAPSHOT,
            outFields: ["*"]
        });

        //地名標註
        var labelSymbol = new TextSymbol().setColor(new Color("#000000"));
        labelSymbol.font.setSize("10pt");
        labelSymbol.font.setFamily("新宋體");
        var json = {
            "labelExpressionInfo": {"value": "{Point}"},
            "useCodedValues": false,
            "labelPlacement":"center-right"
        };
        var labelClass = new LabelClass(json);
        labelClass.symbol = labelSymbol;
        featureLayer.setLabelingInfo([ labelClass ]);
        map.addLayer(featureLayer);
        //添加“update-end”事件
        map.on("update-end", function(){           
        // update-end event will execute after the layer has been added to the map
        if(featureLayer.graphics.length >= 1)
        {
               // do your thing
            console.log(featureLayer.graphics);  
            var point=featureLayer.graphics[0].attributes.Point;
            var num=featureLayer.graphics[0].attributes.NUM;

            console.log("Point:"+point+"    NUM:"+num);         
        }
        });
      });
    </script>
    </head>

    <body>
      <div id="map"></div>
    </body>

</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章