TransWikia.com

ArcGIS API for JavaScript: How to filter layers when using Traffic Service

Geographic Information Systems Asked by Ε Г И І И О on March 6, 2021

I’m using ArcGIS API for JavaScript v4.41 (https://developers.arcgis.com/javascript/)

I want to display a traffic layer on my map with live traffic but without the traffic incidents (red-amber dots). The documentation is found here: https://developers.arcgis.com/rest/network/api-reference/traffic-service.htm.
It says: “To work with the layers in the service, you need to specify the ids for the layers instead of the layer names.”, but I cannot seem to figure out how to pass those layer ids to the service. The code I used to add the traffic layer is below.

Any help regarding this is appreciated. Thank you.

trafficLayer = new MapImageLayer({
      url: "http://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer",
      dpi: 48,
      imageFormat: "png32",
      refreshInterval: 5,
      useViewTime: false
    });

var map = new Map({
      basemap: "streets",
      layers: [trafficLayer]
    });

One Answer

Well I figured it out myself, eventually. It was documented here: https://developers.arcgis.com/javascript/latest/sample-code/layers-mapimagelayer-sublayers/index.html

You have to tell which sublayers you need to be visible. In my case it's the layer 6, which displays live traffic.

const trafficLayer = new MapImageLayer({
    url: "http://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer",
    sublayers: [
        {
          id: 2,
          visible: false
        },
        {
          id: 3,
          visible: false
        },
        {
          id: 4,
          visible: false
        },
        {
          id: 6,
          visible: true
        },
        {
          id: 7,
          visible: false
        }
    ],  
    dpi: 48,
    imageFormat: "png32",
    refreshInterval: 5,
    useViewTime: false
});

Answered by Ε Г И І И О on March 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP