Selecting a Feature after zooming to it

1032
2
Jump to solution
06-17-2013 12:50 PM
Labels (1)
JosephCox
New Contributor II
I am having an issue with selecting a feature after zooming to it. If the feature is within the spatial extent before zooming to it then I am able to successfully select it. If the feature I am zooming to is not within the spatial extent and I zoom to it then selecting the feature fails.

I noticed that the feature Layer only contains features in the spatial extent so when I loop through it to find the feature to select, it is only there if it is currently in the spatial extent. I tried to refresh the feature Layer after zooming to it but it doesn't appear to update to the new extent that I zoomed too. Is there a step that I am missing to force the feature layer to update its list of features after the zoom or is there a way to query all the features in a featureLayer.


Thanks for any help
0 Kudos
1 Solution

Accepted Solutions
GeorgeFaraj
Occasional Contributor III
I am having an issue with selecting a feature after zooming to it. If the feature is within the spatial extent before zooming to it then I am able to successfully select it. If the feature I am zooming to is not within the spatial extent and I zoom to it then selecting the feature fails.

I noticed that the feature Layer only contains features in the spatial extent so when I loop through it to find the feature to select, it is only there if it is currently in the spatial extent. I tried to refresh the feature Layer after zooming to it but it doesn't appear to update to the new extent that I zoomed too. Is there a step that I am missing to force the feature layer to update its list of features after the zoom or is there a way to query all the features in a featureLayer.


Thanks for any help


Assuming that you have your feature layer set to On Demand mode, I had this exact same problem. How do you zoom to the feature? Did you get the feature from a QueryTask? If so, you can just add that feature to the Graphics collection of your feature layer and select it.

Something like:

myLayer.Graphics.Add(feature); feature.Selected = true;


To make it better, you could first check to see if you already have that feature in the Graphics collection, and only add it if it's not there:


var graphic = myLayer.Graphics.FirstOrDefault(g => g.Geometry.Extent.Equals(feature.Geometry.Extent)) if (graphic == null) {   graphic = feature;   myLayer.Graphics.Add(feature); }  graphic.Selected = true;


This worked for me, and I couldn't find any other cleaner way to do it.

-George

View solution in original post

0 Kudos
2 Replies
GeorgeFaraj
Occasional Contributor III
I am having an issue with selecting a feature after zooming to it. If the feature is within the spatial extent before zooming to it then I am able to successfully select it. If the feature I am zooming to is not within the spatial extent and I zoom to it then selecting the feature fails.

I noticed that the feature Layer only contains features in the spatial extent so when I loop through it to find the feature to select, it is only there if it is currently in the spatial extent. I tried to refresh the feature Layer after zooming to it but it doesn't appear to update to the new extent that I zoomed too. Is there a step that I am missing to force the feature layer to update its list of features after the zoom or is there a way to query all the features in a featureLayer.


Thanks for any help


Assuming that you have your feature layer set to On Demand mode, I had this exact same problem. How do you zoom to the feature? Did you get the feature from a QueryTask? If so, you can just add that feature to the Graphics collection of your feature layer and select it.

Something like:

myLayer.Graphics.Add(feature); feature.Selected = true;


To make it better, you could first check to see if you already have that feature in the Graphics collection, and only add it if it's not there:


var graphic = myLayer.Graphics.FirstOrDefault(g => g.Geometry.Extent.Equals(feature.Geometry.Extent)) if (graphic == null) {   graphic = feature;   myLayer.Graphics.Add(feature); }  graphic.Selected = true;


This worked for me, and I couldn't find any other cleaner way to do it.

-George
0 Kudos
JosephCox
New Contributor II
This worked for me as well. Thanks for the help!
0 Kudos