How to zoom to selected feature in ArcGIS Pro SDK Framework

2586
2
Jump to solution
11-23-2020 01:05 PM
Amadeus111
Occasional Contributor II

I am trying to zoom to a selected feature on a feature layer. I have found some answers here but I was not able to adapt any of them into my situation. 

Related answers:

https://community.esri.com/t5/arcgis-pro-sdk-questions/arcgis-pro-sdk-select-feature-from-featurelay...

https://community.esri.com/t5/arcgis-runtime-sdk-for-net/arcgis-runtime-sdk-for-net-zoom-to-features...

https://community.esri.com/t5/arcgis-pro-sdk-questions/pro-sdk-how-to-get-features-after-getfeatures...

 

 

string selectedCounty = CountiesListBox.SelectedItem.ToString();
QueuedTask.Run(() =>
{
   FeatureLayer featLayer = countiesLayer as FeatureLayer;
   QueryFilter qf = new QueryFilter { SubFields = "NAME2" };
   qf.WhereClause = selectedCounty;
   featLayer.Select(qf);
   MapView.Active.ZoomToSelected();
 });

 

 

 

I think I have syntax problem somewhere. If I remove qf I am able to select everything. 

 

 

 

 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi OguzSariyildiz,

 If you surround your code snippet with a try {} catch {} you should see an error message because of your where clause is not a valid where clause.  

The where clause string for a QueryFilter has to be specified in sql where clause format:

 

{
			string selectedCounty = "San Bernardino";
			QueuedTask.Run(() =>
			{
				var featLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains("Counties")).FirstOrDefault();
				QueryFilter qf = new QueryFilter {
					WhereClause = $@"name = '{selectedCounty}'"
				};				
				featLayer.Select(qf);
				MapView.Active.ZoomToSelected();
			});
		}

 

 

 

View solution in original post

2 Replies
by Anonymous User
Not applicable

Hi OguzSariyildiz,

 If you surround your code snippet with a try {} catch {} you should see an error message because of your where clause is not a valid where clause.  

The where clause string for a QueryFilter has to be specified in sql where clause format:

 

{
			string selectedCounty = "San Bernardino";
			QueuedTask.Run(() =>
			{
				var featLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains("Counties")).FirstOrDefault();
				QueryFilter qf = new QueryFilter {
					WhereClause = $@"name = '{selectedCounty}'"
				};				
				featLayer.Select(qf);
				MapView.Active.ZoomToSelected();
			});
		}

 

 

 

Amadeus111
Occasional Contributor II

The idea was zooming to selected feature when user selects it via a listbox. I knew I was doing something wrong with the whereclause so it worked when I changed the code as 

 

string selectedCounty = CountiesListBox.SelectedItem.ToString();
QueuedTask.Run(() =>
{
   FeatureLayer featLayer = countiesLayer as FeatureLayer;
   QueryFilter qf = new QueryFilter { WhereClause = $@"myFieldName = 
                                    '{selectedCounty}'" };				
   qf.WhereClause = selectedCounty;
   featLayer.Select(qf);
   MapView.Active.ZoomToSelected();
 });

 

OguzSariyildiz_0-1606167963401.png

Thanks @Anonymous User  

0 Kudos