Select to view content in your preferred language

FindGraphicsInHostCoordinates problem

1704
6
12-03-2010 02:04 AM
FilipJung
Frequent Contributor
Hi,
I'm trying to use FindGraphicsInHostCoordinates on FeatureLayer after LeftMouseButtonDown on layer feature. The code is as follows:

Point position = e.ScreenPoint;
FeatureLayer featureLayer = MyMap.Layers["Stops"] as FeatureLayer;
IEnumerable<Graphic> clickedFeatures = featureLayer.FindGraphicsInHostCoordinates(position);
MessageBox.Show(clickedFeatures.Count().ToString());


Everything goes OK when I zoom on the map, but when I pan to new location and click on the feature I got an number of features '0'. When I zoom out or in and click that feature again, I got '1' which is of course correct.

What is the problem here? I firstly thought that problem is that layer property Mode was set to "OnDemand" so I switched to "Snapshot" but it hadn't helped. Thanks for help.

Filip
0 Kudos
6 Replies
DominiqueBroux
Esri Frequent Contributor
I guess you missed the transformation to host coordinates.
Please look at Morten's answer in this thread : http://forums.arcgis.com/threads/3093-Graphic-Selection-based-on-Draw-Envelope-issue
0 Kudos
FilipJung
Frequent Contributor
I guess you missed the transformation to host coordinates.
Please look at Morten's answer in this thread : http://forums.arcgis.com/threads/3093-Graphic-Selection-based-on-Draw-Envelope-issue


Hi,
I've looked at suggested post and tried following:
Point position = e.ScreenPoint;
Point screenPointRelativeToHost = MyMap.TransformToVisual(Application.Current.RootVisual).Transform(position);


But 'position' and 'screenPointRelativeToHost' return the same coords. How can I get the right coords? They are right returned when I use: Point pos = args.GetPosition(MyMap); after clicking on FeatureLayer. Can I get right coords when clicking in the map after panning?

I want to solve situation, where graphic lies on features (dynamic layer) that are in identify task, but I don't want to execute identify task, if there is graphic in click location.

Thanks.
0 Kudos
JenniferNery
Esri Regular Contributor
Are you using Silveright, WPF or WP7?

This should work for Silverlight:
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;  
GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);
System.Windows.Point transformScreenPnt = generalTransform.Transform(e.GetPosition(this.MyMap));
var graphics = graphicsLayer.FindGraphicsInHostCoordinates(transformScreenPnt);


The following examples demonstrate the use of FindGraphicsInHostCoordinates:
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#InfoWindowSimple
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Offset
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Reshape
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#Union
0 Kudos
FilipJung
Frequent Contributor
I'm using Silverlight. I've used code used in webapi example and it works fine:

MapPoint pointInMap= e.MapPoint;
Point clickPosition= MyMap.MapToScreen(pointInMap);
GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);
Point transformScreenPnt = generalTransform.Transform(clickPosition);
var graphics = graphicsLayer.FindGraphicsInHostCoordinates(transformScreenPnt);


Thanks very much for Your help, it really helped me much solving my issue. Next time I should carefully check all examples in webapi 😉
0 Kudos
JustinCornell
Occasional Contributor
Hi, I am using WPF and I trying the same thing, however,  I can only get the top most graphic returned.  Is there something different about silverlight than WPF?  Here is what I have tried and they both return to me on the top most graphic when I need all stacked graphics. (I am using version 2.4.0.851 of the API) If you have some code that will return all graphics please post.


//only returns the top most graphic
System.Windows.Media.GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.MainWindow);
Point RightMouseButtonDownLocation = generalTransform.Transform(e.GetPosition(MyMap));

var graphics = MyFeatureLayer.FindGraphicsInHostCoordinates(RightMouseButtonDownLocation).ToList();


///----------- TRIED THIS WAY TOO -----------
//only returns the top most graphic
Point RightMouseButtonDownLocation = e.GetPosition(Application.Current.MainWindow);
var graphics = MyFeatureLayer.FindGraphicsInHostCoordinates(RightMouseButtonDownLocation).ToList();



Any help would be appreciated.  Thanks!
0 Kudos
JustinCornell
Occasional Contributor
So I answer my own question...

It appears there is a bug in the WPF API for the FindGraphicsInHostCoordinates (WPF API Version: 2.4.0.851).  (If it's not a bug, then it's not documented to have the following characteristics)

After tracking the down the documentation on the WPF version of FindGraphicsInHostCoordinates , I noticed the API says it's exepecting a Rect for the input parameters.  Passing in a point or a Rect with a width/height of 0 will only return the topmost graphic.  So the fix is to create a rectangle out of the clicked point relative to the application.  Then use the Inflate function to give the rectangle any width/height greater than 0.  Here is what the code would look like.

Point RightMouseButtonDownLocation = e.GetPosition(Application.Current.MainWindow);

Rect rect = new Rect(RightMouseButtonDownLocation, RightMouseButtonDownLocation);
rect.Inflate(1,1); //THE FIX -inflate making the box 1px x 1px

var graphics = MyFeatureLayer.FindGraphicsInHostCoordinates(rect);



Hope this helps.
0 Kudos