Select to view content in your preferred language

how to retrieve a graphic that contains a drawing point

5212
15
12-17-2010 10:22 AM
ThaoNguyen
Emerging Contributor
My scenario:
- I have a drawing "point" icon for my map.  When I click on a region in US, I want to highlight that region.  I can get the highlight done.  When clicking on a region, I don't want to run the query task again to get the region that contains the point.  So, my design is I go thru the existing graphic list and find the region that "intersects" with that point, then do the highlight.
Something like this:

if (graphic.Geometry.Extent.Intersects(e.Geometry.Extent))
{                   
      graphic.Select();                   
}

e is DrawEventArgs.  graphic is from graphic layer on the map.

However, I got a problem that sometimes 2 regions contain that point and the result is both of them are selected.  Please see the attached image, my clicking point is at the crossed symbol x. When I click at that point, both regions A and B are selected while I only want region B is selected.
Is there a way to fix this?  Looks like to me graphic geometry is a rectangle and that causes the point to be inside both regions in the image?

I appreciate your help!
0 Kudos
15 Replies
DominiqueBroux
Esri Frequent Contributor
The main window won't be the grid inside your control. It could be the control itself if it's used as main window.
0 Kudos
ThaoNguyen
Emerging Contributor
You can do the following in the MouseClick event of your Map control to get the graphics that intersect your MapPoint.

Assumptions in the code snippet:
- MyMap: The Map control in your application
- MyFeatureLayer: The FeatureLayer in your map you are intersecting the map point
- MyGraphicsLayer: The layer to show the intersecting result(s)


FeatureLayer featureLayer = MyMap.Layers["MyFeatureLayer"] asFeatureLayer;
 
System.Windows.Point screenPnt = MyMap.MapToScreen(e.MapPoint);
GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);
System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);
IEnumerable<Graphic> results = featureLayer.FindGraphicsInHostCoordinates(transformScreenPnt);

GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] asGraphicsLayer;

foreach (Graphic graphic in results)
{
Graphic g = newGraphic() { Geometry = graphic.Geometry };
graphicsLayer.Graphics.Add(g);
}


I'm just back to this.  I don't have e.MapPoint but having DrawEventArgs e which can give me Geometry.  How do I create a MapPoint or Point using Geometry?

I tried the following but looks like it's not correct because FindGraphicsInHostCoordinates() does not return me graphics sometimes.  I'm using this method from graphic layer, not feature layer.

System.Windows.Point screenPnt = MainMap.MapToScreen(new MapPoint(e.Geometry.Extent.XMax,e.Geometry.Extent.YMax));


Thanks!
0 Kudos
JenniferNery
Esri Regular Contributor
You can use Intersects() method http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Geometry.E...

Something like this, where args.Geometry is the MapPoint created by Draw. This code-snippet allows me to select all graphics that contain the MapPoint.
foreach (var g in graphicsLayer.Graphics)
{
 if (g.Geometry.Extent.Intersects(args.Geometry.Extent))
  g.Selected = true;
}
0 Kudos
ThaoNguyen
Emerging Contributor
You can use Intersects() method http://help.arcgis.com/en/webapi/silverlight/apiref/ESRI.ArcGIS.Client~ESRI.ArcGIS.Client.Geometry.E...

Something like this, where args.Geometry is the MapPoint created by Draw. This code-snippet allows me to select all graphics that contain the MapPoint.
foreach (var g in graphicsLayer.Graphics)
{
 if (g.Geometry.Extent.Intersects(args.Geometry.Extent))
  g.Selected = true;
}


That is what I'm doing and it is why I posted this question.  I had what you had above in my first post in this thread.  It does NOT give the right result as stated in my first post :).
0 Kudos
JenniferNery
Esri Regular Contributor
Oh, could you verify that the geometries have the same SpatialReference as the MapPoint? If they are found to be not equal, Intersects() will return false.
0 Kudos
ThaoNguyen
Emerging Contributor
Oh, could you verify that the geometries have the same SpatialReference as the MapPoint? If they are found to be not equal, Intersects() will return false.


Yes, they have the same SpatialReference.  It's just that the result returned is more than what I draw.  You can look at the image I attached in the 2nd post.  Intersects() won't work in all cases.
0 Kudos