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:
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.
Solved! Go to Solution.
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();
});
}
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();
});
}
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();
});
Thanks @Anonymous User