Passing a mouse click to do a query in Viewer

2234
8
Jump to solution
04-10-2012 12:45 PM
ThomasMcCracken
New Contributor III
Hello everyone in Flex Viewer world,

I'm using the Flex API to create my own query widget.  Part of the functionality I want is to click a polygon and perform the query like so in the Flex API:  http://help.arcgis.com/en/webapi/flex/samples/index.html#/Select_and_zoom/01nq0000003s000000/

Ok, this functionality is enabled partly by this markup:  <esri:Map id="myMap" mapClick="mapClickHandler(event)">

Since I'm unable to simply add a markup to the Flex Viewer map for mapClick, how can I pass the map click event to the mapClickHandler function to work the same way?  I know the Viewer does this with the pop-ups but I'm unable to see how.

If anyone has any ideas or perhaps a couple snippets of code as an example I would appreciate it, thanks!

Thomas McCracken
GIS Analyst
Georgia Power
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus
Thomas,

  If you look at all other widgets you will notice that they DO NOT interact with the map by adding a mapClick event instead they use the drawTool and get the geometry (in your case a MapPoint) returned and then work with that. You can search most widgets for setMapAction and you will see how that function (built into all widgets that extend baseWidget) is expecting an action (i.e. DrawTool.MAPPOINT), a status (text can be ""), a symbol (to draw on the map), and a function to return the drawEvent to (where you use event.graphic.geometry to get the MapPoint drawn). Once you have the MapPoint (Geometry) you can see how this ties right back into the sample code you referenced.

Don't forget to click the Mark as answer check on this post and to click the top arrow (promote) as shown below:

View solution in original post

0 Kudos
8 Replies
RobertScheitlin__GISP
MVP Emeritus
Thomas,

  If you look at all other widgets you will notice that they DO NOT interact with the map by adding a mapClick event instead they use the drawTool and get the geometry (in your case a MapPoint) returned and then work with that. You can search most widgets for setMapAction and you will see how that function (built into all widgets that extend baseWidget) is expecting an action (i.e. DrawTool.MAPPOINT), a status (text can be ""), a symbol (to draw on the map), and a function to return the drawEvent to (where you use event.graphic.geometry to get the MapPoint drawn). Once you have the MapPoint (Geometry) you can see how this ties right back into the sample code you referenced.

Don't forget to click the Mark as answer check on this post and to click the top arrow (promote) as shown below:
0 Kudos
ThomasMcCracken
New Contributor III
Robert,

I was having trouble implementing this solution so I then added a map Event Listener for a map click and then passed that event to the handler and everything ran fine.  Do you see a downside to this approach?  It was a very easy solution once I figured out where to add it.

Thanks,

Thomas
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Thomas,

  Yes the down side is that if your widget is opened it has the listener added to the map and then the user also opens another widget that activates the viewer drawTool they both will be executing on the map click event. If you do it the way I describe then when a new widget is opened and the setMapAction is called there is an event fired normally that deactivates other widgets listeners to the map.
0 Kudos
ThomasMcCracken
New Contributor III
Yes, I see what you mean with the other widgets map clicks....well, so I need to call that setMapAction method in my mapClickHandler function then and create the map graphic in another function which that method calls and from there I can pass that event.graphic.geometry on to my query.geometry and then queryTask methods?  The way the API works  and the way the Viewer widgets work is very different.  I'm looking in the Search and Draw widgets and I think I'm missing how the map mouse click initiates these functions with a map event listener...

Sorry for the confusion, but I am very new to this.  If it's too much trouble you don't have to keep responding.

Thanks,

Thomas
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Thomas,

  You don't see the mapclick event interaction because that is all hidden (included in the DrawTool). What you will find in the draw widget is there is a button that fires the setMapAction and the MapManager actually has the DrawTool defined and it adds and removes map click event listeners internally. Hope that sheds a little more light on this for you. So you lay out a road map for your widget, it would need a button that fires the activateDrawTool function below:

import com.esri.ags.tools.DrawTool;
import com.esri.ags.events.DrawEvent;
import com.esri.ags.geometry.MapPoint;

            private function activateDrawTool(event:MouseEvent):void
            {
                addSharedData("Deactivate_DrawTool", null); // to be able to deactivate drawTool on other widgets
                setMapAction(DrawTool.MAPPOINT, "My tool that does something", new SimpleMarkerSymbol("circle",12,0x0000ff,0.8), DrawEnd);
            }

            private function DrawEnd(event:DrawEvent):void
            {
                //deactivates the drawTool
                event.target.deactivate();
                var geom:Geometry = event.graphic.geometry;
                //If you want to persist the point on the map
                var gra:Graphic = new Graphic(geom, new SimpleMarkerSymbol("circle",12,0x0000ff,0.8));
                graphicsLayer.add(gra);

                //snippet of code from the smaple
                query.geometry = geom;
                            queryTask.execute(query);
            }
0 Kudos
ThomasMcCracken
New Contributor III
Ah, it's all internal.  I see now.  Thanks Robert! I believe I can get it working now!
0 Kudos
AshleyOwens
New Contributor II
Is there a way to programmatically mimic the action of a user clicking a search result so the map is zoomed to the result?
0 Kudos
AshleyOwens
New Contributor II
Is there a way to programmatically mimic the action of a user clicking a search result so the map is zoomed to the result?

For anyone interested, I accomplished this by adding zoom logic to my onResult function within the queryFeaturesText function.
0 Kudos