Programmatically fire map's click event

794
5
10-21-2022 11:08 AM
Lzy
by
New Contributor III
Hello,

I am using the Esri's Javascript API 4.24. I have created functionality that allows a user to add a graphic to the map by clicking on the map. The type of graphic added depends on some previous selection that user has made. For example, selecting one option (say Option A) adds a circle graphic whereas selecting a different option (say Option B) adds a square graphic.

So, my question is how can i programmatically fire/trigger a map's click event?
0 Kudos
5 Replies
JoelBennett
MVP Regular Contributor

Technically, you'd be able to do this by calling MapView.emit, and passing as close to an event object as you can. This could be problematic though, since simulating the "stopPropagation" function and the "native" property could present certain inconsistencies.

Probably the easiest approach would be something like this:

function simulateClick(mapView, mapPoint) {
	var screenPoint = mapView.toScreen(mapPoint);

	mapView.emit("click", {
		mapPoint: mapPoint,
		x: screenPoint.x,
		y: screenPoint.y,
		button: 0,
		buttons: 0,
		type: "click",
		stopPropagation: function() { },
		timestamp: Date.now(),
		native: {}
	});
}

 

As a disclaimer, I would never do this in practice, or recommend it either...I'm just answering the question as asked.

Lzy
by
New Contributor III

Thanks, it's a good idea。

If you use sketchViewModel API to draw polygons, can you click the button to punctuate with emit. How can this be achieved?

0 Kudos
JoelBennett
MVP Regular Contributor

I don't know the answer to that.  Since it's off-topic, you might consider posting it as a separate question for greater visibility.

0 Kudos
MichaelSnook
Occasional Contributor III

Hi...any luck on this?  No matter what I try I can't seem to get the click to fire.  The mapview.emit does not seem to work in this case.  I am trying to get a forced map click to run an identify programatically.

 

Thanks.

0 Kudos
MichaelSnook
Occasional Contributor III

Well shoot...I just figured that out:

mapView.popup.open({ 
                location: event.result.feature.geometry, // <- use map point of the click event
                fetchFeatures: true // <- fetch the selected features (if any)
            });

 It still would be nice to fire the click event though... 

0 Kudos