Select to view content in your preferred language

Has anyone used "findLayerByID" with success?

274
2
Jump to solution
07-29-2024 05:49 PM
Paco
by
Frequent Contributor

Hello again all.    I am still having problems accessing individual layers within my WebMap thru the js API.

if I have loaded an existing webmap visually in my app-

const webmap = new WebMap({
  portalItem: { // autocasts as new PortalItem()
    id: "xxportalIDxx"
  }
});
 
why can't I access the individual Layers within it by their IDs?   i've tried-
 
const newLayer = webmap.findLayerByID('xxlayerIDxx')
 
but keep getting- 
 
TypeError: webmap.findLayerByID is not a function
 
shouldn't this be a basic chore,  to access individual layers within a WebMap?
 
thx!
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Honored Contributor

I think you have a typo. It's findLayerById.

You need to either wait for view.when() or call the webmap.loadAll(), both are promises so you can await them or use promise.then().

View solution in original post

2 Replies
ReneRubalcava
Honored Contributor

I think you have a typo. It's findLayerById.

You need to either wait for view.when() or call the webmap.loadAll(), both are promises so you can await them or use promise.then().

Paco
by
Frequent Contributor

Ah yes,  a lower-case "D" thank you.

okay,  "view.when" is where I went and it seems to work fine.  thanks everyone!

here is the way I did it,  using the layer 'title'.   and the code for a dropdown checkbox for the layers visible/not visible.

view.when(function() {
orientationLayer = webmap.allLayers.find(function(layer) {
  return layer.title === "SHmax orientations";
});
console.log(orientationLayer)
rasterpolyLayer = webmap.allLayers.find(function(layer) {
  return layer.title === "NA_Aphi_NAAP40";
});
console.log(rasterpolyLayer)
});

//Layers dropdown
document.getElementById("orientationLayer").addEventListener('click', function() {
  console.log("orientation layer")
  if (this.checked == true) {
    orientationLayer.visible = true
  }
  else orientationLayer.visible = false
  });
document.getElementById("rasterpolyLayer").addEventListener('click', function() {
  console.log("raster poly layer")
  if (this.checked == true) {
    rasterpolyLayer.visible = true
  }
  else rasterpolyLayer.visible = false
  });
0 Kudos