Get value of an attribute from GraphicsLayer

745
3
Jump to solution
05-04-2021 07:37 AM
iepshteyn
New Contributor

I am adding a GraphicsLayer with markers on the map and need to know which marker was clicked. How do I get a value of an attribute when I click on the marker? Thank you

const Layer1 = new GraphicsLayer({id: 'Layer1ID'});
map.add(Layer1);
const point = {
type: "point",
longitude: -80,
latitude: 25
};
const attributes = { "MarkerType": "Type1" }
const pointGraphic = new Graphic({
geometry: point,
symbol: simpleMarkerSymbol,
attributes: attributes
});
Layer1.add(pointGraphic);

view.on(""click"", e => {
alert(need to access the above attribute MarkerType);
});

  

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You can use this code to get your Markertype

view.on('click',(event) =>{
  view.hitTest(event)
    .then((response) =>{
      console.log(response.results[0].graphic.attributes.MarkerType); //or attributes['MarkerType'];
    });
});

View solution in original post

0 Kudos
3 Replies
BlakeTerhune
MVP Regular Contributor

hitTest() will return features from an intersecting GraphicsLayer, from which you can getAttribute() or browse with the attributes property.

Here are some hitTest() samples to check out.

0 Kudos
KenBuja
MVP Esteemed Contributor

You can use this code to get your Markertype

view.on('click',(event) =>{
  view.hitTest(event)
    .then((response) =>{
      console.log(response.results[0].graphic.attributes.MarkerType); //or attributes['MarkerType'];
    });
});
0 Kudos
iepshteyn
New Contributor

Thank you Ken.

0 Kudos