0

ユーザーが HTML チェック ボックスを使用してレイヤーのオンとオフを切り替えることができる JavaScript API を使用して、ArcGIS online から Web マップ アプリケーションにフィーチャ レイヤーを追加しようとするプロジェクトに取り組んでいます。レイヤーは正しくインポートされており、チェックボックスをバイパスすると表示されますが、チェックボックスで動作させることができます。私は ArcGIS のサンプルなどからコードをハッキングしました。

ここにコードがあります - 基本的に私が欲しいのは、ユーザーがチェックボックスをオンまたはオフにすることに基づいて、一定のベースマップの上でオンとオフを切り替えるレイヤーだけです

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Select with feature layer</title>
    <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/tundra/tundra.css">
    <link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
    <style>
      html, body, #mapDiv {
        padding: 0;
        margin: 0;
        height: 100%;
      }
      #messages{
        background-color: #fff;
        box-shadow: 0 0 5px #888;
        font-size: 1.1em;
        max-width: 15em;
        padding: 0.5em;
        position: absolute;
        right: 20px;
        top: 20px;
        z-index: 40;
      }
    </style>
    <script src="http://js.arcgis.com/3.10/"></script>
    <script>

        var map;
        require([
        "esri/map", "esri/layers/FeatureLayer",
        "esri/tasks/query", "esri/geometry/Circle",
        "esri/graphic", "esri/InfoTemplate", "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol", "esri/symbols/SimpleFillSymbol", "esri/renderers/SimpleRenderer",
        "esri/config", "esri/Color", "dojo/dom", "dojo/domReady!", "dojo/on", "dojo/query", "dojo/domReady!", "esri/layers/ArcGISDynamicMapServiceLayer", "esri/layers/ImageParameters",
      ], function (
        Map, FeatureLayer,
        Query, Circle,
        Graphic, InfoTemplate, SimpleMarkerSymbol,
        SimpleLineSymbol, SimpleFillSymbol, SimpleRenderer,
        esriConfig, Color, dom, on, query, ArcGISDynamicMapServiceLayer, ImageParameters
      ) {
          // use a proxy page if a URL generated by this page is greater than 2000 characters
          //
          // this should not be needed as nearly all query & select functions are performed on the client
          esriConfig.defaults.io.proxyUrl = "/proxy";

          map = new Map("mapDiv", {
              basemap: "streets",
              center: [-120.303130, 36.542750],
              zoom: 5,
              slider: false
          });

          //add the census block points in on demand mode. Note that an info template has been defined so when
          //selected features are clicked a popup window will appear displaying the content defined in the info template.
          var Offices = new FeatureLayer("URL", {
              infoTemplate: new InfoTemplate("Block: ${BLOCK}", "${*}"),
              outFields: ["*"]
          });

          var Northridge = new FeatureLayer("URL", {
              infoTemplate: new InfoTemplate("Block: ${BLOCK}", "${*}"),
              outFields: ["*"]
          });

          var Associates = new FeatureLayer("URL", {
              infoTemplate: new InfoTemplate("Block: ${BLOCK}", "${*}"),
              outFields: ["*"]
          });

          // selection symbol used to draw the selected census block points within the buffer polygon
          var symbol = new SimpleMarkerSymbol(
          SimpleMarkerSymbol.STYLE_CIRCLE,
          12,
          new SimpleLineSymbol(
            SimpleLineSymbol.STYLE_NULL,
            new Color([247, 34, 101, 0.9]),
            1
          ),
          new Color([207, 34, 171, 0.5])
        );
          Offices.setSelectionSymbol(symbol);

          //make unselected features invisible
          var nullSymbol = new SimpleMarkerSymbol().setSize(10);
          var RedCircle = new SimpleMarkerSymbol().setSize(8);

          Offices.setRenderer(new SimpleRenderer(nullSymbol));

          on(dom.byId("layer0CheckBox"), "change", updateLayerVisibility);
          on(dom.byId("layer1CheckBox"), "change", updateLayerVisibility);
          on(dom.byId("layer2CheckBox"), "change", updateLayerVisibility);

          function updateLayerVisibility() {
              var inputs = query(".list_item");
              var inputCount = inputs.length;
              //in this application layer 2 is always on.
              visibleLayerIds = [2];

              for (var i = 0; i < inputCount; i++) {
                  if (inputs[i].checked) {
                      visibleLayerIds.push(inputs[i].value);
                  }
              }

              if (visibleLayerIds.length === 0) {
                  visibleLayerIds.push(-1);
              }

              map.addLayers(visibleLayerIds);
          }
      });

    </script>
  </head>

  <body>
<br />
    <br />
        Layer List : <span id="layer_list"><input type='checkbox' class='list_item' id='layer0CheckBox' value="Northridge" />Earthquake  
          <input type='checkbox' class='list_item' id='layer1CheckBox' value="Offices" />Offices
          <input type='checkbox' class='list_item' id='layer2CheckBox' value="Associates" />Associates
        </span><br />
        <br />


    <div id="mapDiv"></div>

  </body>
</html>
4

2 に答える 2

0
  1. 関数 updateLayerVisibility で、map.addLayers の使用法が間違っています。配列 visibleLayerIds に格納しない限り、id ではなく、フィーチャ レイヤー、グラフィック レイヤー、動的レイヤー、またはタイル レイヤーを追加する必要があります。

  2. ArcGISDynamicMapServiceLayer を使用して、表示されるレイヤー ID を制御することを検討してください。次に、setVisibleLayers API を使用します。https: //developers.arcgis.com/javascript/jsapi/arcgisdynamicmapservicelayer-amd.html を参照してください。

  3. FeatureLayer は通常、単一レイヤー用で、MapServer/2 や FeatureServer/2 などのサービスを使用します。

于 2016-03-30T05:12:24.220 に答える