I want to make an event fire upon clicking a GraphicsLayer. I have tried to get this to work with the following code:
var symbolsLayer = new GraphicsLayer();
...
symbolsLayer.on("click", function (event) {
// event is the event handle returned after the event fires.
console.log(event.mapPoint);
debugger;
});
The event is never fired - but there is no error in the console. What is the correct way to do this?
Samuel,
Is this 3.x or 4.x API?
Hi there, I am using the new 4.x API
Samuel,
So if you are using 4.x then as Oren mentioned above you have to use hitTest.
// Set up a click event handler and retrieve the screen point
view.on("click", function(evt) {
var screenPoint = evt.screenPoint;
// the hitTest() checks to see if any graphics in the view
// intersect the given screen point
view.hitTest(screenPoint)
.then(getGraphics);
});
function getGraphics(response) {
// the topmost graphic from the click location
// and display select attribute values from the
// graphic to the user
console.info(response.results);
var graphic = response.results[0].graphic;
var attributes = graphic.attributes;
...
Hi, Please try:
myMapView.on("click", function (evt) {
var screenPoint = {
x: evt.x,
y: evt.y
};
myMapView.hitTest(screenPoint).then(function (response) {
$.each(response.results, function (index, re) {
var id = re.graphic.layer.id;
if (id === "graphicslayername") {
//do something
}
});
});
});