MapImageLayer Sublayer Geomtery Type

867
4
Jump to solution
11-17-2017 08:26 AM
RodrigoFelga
New Contributor III

Whats is the way (if exists) to get the geometry type (point, line, polygon) of a MapImageLayerSublayer? I'm using js API 4.5. The way i'm doing is going on the server again and geting  the json of the layer to get the geometry type

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

OK, from you post I was not sure if you were aware of the REST Layers endpoint for getting all map service layers or if you were doing a server round trip for each layer. As I mentioned there is not current way to do this without a service trip.

View solution in original post

4 Replies
RobertScheitlin__GISP
MVP Emeritus

Rodrigo,

   I am not aware of any property to do this in 4.5 but you can just take the url of the whole MapService and append "layers" to the end of the url and do an esriRequest and parse the results json for layers.geometryType.

Here is a sample:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>MapImageLayer - set renderers on sublayers - 4.4</title>

  <link rel="stylesheet" href="https://js.arcgis.com/4.4/esri/css/main.css">
  <script src="https://js.arcgis.com/4.4/"></script>

  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <script>
    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/MapImageLayer",
        "esri/renderers/SimpleRenderer",
        "esri/symbols/SimpleMarkerSymbol",
        "esri/symbols/SimpleLineSymbol",
        "esri/symbols/SimpleFillSymbol",
        "esri/request",
        "dojo/domReady!"
      ],
      function(
        Map, MapView, MapImageLayer, SimpleRenderer,
        SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, esriRequest
      ) {

        /*****************************************************************
         * Create renderers for each sublayer: cities, highways, states
         *****************************************************************/

        var citiesRenderer = new SimpleRenderer({
          symbol: new SimpleMarkerSymbol({
            size: 3,
            color: "white",
            style: "circle",
            outline: {
              width: 0.5,
              color: "gray"
            }
          }),
          label: "Cities"
        });

        var statesRenderer = new SimpleRenderer({
          symbol: new SimpleFillSymbol({
            style: "none",
            outline: {
              width: 0.5,
              color: "gray"
            }
          }),
          label: "State boundaries"
        });

        var highwaysRenderer = new SimpleRenderer({
          symbol: new SimpleLineSymbol({
            style: "none",
            width: 0.7,
            color: "white"
          }),
          label: "Interstate highway"
        });

        /*****************************************************************
         * Create a MapImageLayer instance pointing to a Map Service
         * containing data about US Cities, States and Highways.
         * Define sublayers with visibility for each layer in Map Service.
         *****************************************************************/
        esriRequest("https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/layers?f=json", {
          responseType: "json"
        }).then(function(response){
          response.data.layers.forEach(function(layer){
            console.info(layer.name + ": " + layer.geometryType);
          });
        });
        var layer = new MapImageLayer({
          url: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
          sublayers: [
          {
            id: 2,
            renderer: statesRenderer,
            opacity: 0.4
          },
          {
            id: 1,
            renderer: highwaysRenderer,
            opacity: 0.8
          },
          {
            id: 0,
            renderer: citiesRenderer,
            opacity: 0.6
          }]
        });

        /*****************************************************************
         * Add the layer to a map
         *****************************************************************/

        var map = new Map({
          basemap: "dark-gray",
          layers: [layer]
        });

        var view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 6,
          center: [-83.114, 36.921]
        });

      });
  </script>

</head>

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

</html>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Rodrigo,

   Did the sample I posted help?

0 Kudos
RodrigoFelga
New Contributor III

I was already doing it that way, I'm wondering if is possible do do that without another roundtrip to the server. Thank you for your reply

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

OK, from you post I was not sure if you were aware of the REST Layers endpoint for getting all map service layers or if you were doing a server round trip for each layer. As I mentioned there is not current way to do this without a service trip.