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!
System.Windows.Point screenPoint = args.GetPosition(MyMap); ESRI.ArcGIS.Client.Geometry.MapPoint mapPoint = MyMap.ScreenToMap(screenPoint);
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);
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); }
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); }
GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.MainWindow); System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt); IEnumerable<Graphic> selected = parcelGraphicsLayer.FindGraphicsInHostCoordinates(transformScreenPnt);
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.