How do I create a click event on a dynamically created pointGraphic?

967
3
Jump to solution
08-12-2017 01:27 PM
MarceloAlmeida
New Contributor

I'm developing a simple application where I have a map and for each user who logs a point is added on the map. I can not create the click event when I click on one of these points and open information about it.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

You simply add the click event to the GraphicsLayer that you're adding your points.

var GLayer = new GraphicsLayer();
GLayer.on("click", runClickEvent);
function runClickEvent(evt){
    //do something with the event
}

There is another way to do this, in addition. When you construct the graphic, you can add an InfoTemplate (in 3.x) or PopupTemplate (in 4.x). See this sample that shows how to incorporate the attributes and PopupTemplate into a graphics that is added to a map (in this case, the line).

View solution in original post

3 Replies
KenBuja
MVP Esteemed Contributor

The graphic itself can't get a click event, but you have to add that graphic to a layer, which does have a click event. Simply listen to the click event for that layer.

MarceloAlmeida
New Contributor

got it. and how I do it? 

0 Kudos
KenBuja
MVP Esteemed Contributor

You simply add the click event to the GraphicsLayer that you're adding your points.

var GLayer = new GraphicsLayer();
GLayer.on("click", runClickEvent);
function runClickEvent(evt){
    //do something with the event
}

There is another way to do this, in addition. When you construct the graphic, you can add an InfoTemplate (in 3.x) or PopupTemplate (in 4.x). See this sample that shows how to incorporate the attributes and PopupTemplate into a graphics that is added to a map (in this case, the line).