Select to view content in your preferred language

how to retrieve a graphic that contains a drawing point

5114
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
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!


Attach the image.
0 Kudos
JenniferNery
Esri Regular Contributor
You can try this SDK sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SpatialQuery. It uses QueryTask based on the geometry (i.e. MapPoint if DrawMode is Point). You can modify this sample, you will not need a Draw object. You can obtain MapPoint through any of the mouse events.

  
System.Windows.Point screenPoint = args.GetPosition(MyMap);
ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = MyMap.ScreenToMap(screenPoint);
0 Kudos
ThaoNguyen
Emerging Contributor
You can try this SDK sample http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SpatialQuery. It uses QueryTask based on the geometry (i.e. MapPoint if DrawMode is Point). You can modify this sample, you will not need a Draw object. You can obtain MapPoint through any of the mouse events.

  
System.Windows.Point screenPoint = args.GetPosition(MyMap);
ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = MyMap.ScreenToMap(screenPoint);


Jennifer,
Like I said, I didn't want to run QueryTask again.  I had looked at that spatial query before posting this question.  I just want to use the exisitng graphics that I already query earlier and look for the one that contains the point.  Looks like there is no way to get the right graphic WITHOUT running QueryTask?
0 Kudos
JenniferNery
Esri Regular Contributor
How about using FindGraphicsInHostCoordinates? You can look at the MouseClick event handler in this sample: http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#InfoWindowSimple
0 Kudos
AliMirzabeigi
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);
}
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);
}


Thanks Jennifer and Ali! Looks like that can resolve my problem, but I cannot test it because I cannot find the property RootVisual from Application.Current.  I tried Application.Current.MainWindow but that is null.
0 Kudos
JenniferNery
Esri Regular Contributor
If you are using WPF:
GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.MainWindow);
System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);

IEnumerable<Graphic> selected =
                parcelGraphicsLayer.FindGraphicsInHostCoordinates(transformScreenPnt);
0 Kudos
ThaoNguyen
Emerging Contributor
If you are using WPF: 
GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.MainWindow);
System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);

IEnumerable<Graphic> selected =
                parcelGraphicsLayer.FindGraphicsInHostCoordinates(transformScreenPnt);


I used MainWindow, but it always returned me null.
0 Kudos
ThaoNguyen
Emerging Contributor
I used MainWindow, but it always returned me null.


I found out why it is always null for my case.

Another question. My xaml has something like this:
<UserControl>
<UserControl.Resources/>
<Grid>my map is in here</Grid>
</UserControl>

Is that true that the MainWindow would be the Grid element in my xaml?
0 Kudos