private static async Task<KeyValuePair<Layer,IEnumerable<IdentifyFeature>>> IdentifyLayer(MapViewController controller, Layer layer, Point tapPoint, MapPoint mapPoint)
{
if (layer is ArcGISDynamicMapServiceLayer)
{
var dynamicLayer = ((ArcGISDynamicMapServiceLayer)layer);
if (!dynamicLayer.ServiceInfo.Capabilities.Contains("Query"))
return new KeyValuePair<Layer, IEnumerable<IdentifyFeature>>(layer, null);
var identifyTask = new IdentifyTask(new Uri(dynamicLayer.ServiceUri, UriKind.Absolute));
var resolution = controller.UnitsPerPixel;
var center = controller.Extent.GetCenter();
var extent = new Envelope(center.X - resolution * c_defaultTolerance, center.Y - resolution * c_defaultTolerance,
center.X + resolution * c_defaultTolerance, center.Y + resolution * c_defaultTolerance, controller.SpatialReference);
var identifyParameter = new IdentifyParameters(mapPoint, extent, c_defaultTolerance, c_defaultTolerance,
c_defaultTolerance, DisplayInformation.GetForCurrentView().LogicalDpi)
{
LayerOption = LayerOption.Visible,
LayerTimeOptions = dynamicLayer.LayerTimeOptions,
LayerIDs = dynamicLayer.VisibleLayers,
DynamicLayerInfos = dynamicLayer.DynamicLayerInfos,
GeodatabaseVersion = dynamicLayer.GeodatabaseVersion,
TimeExtent = controller.TimeExtent,
};
var identifyItems = (await identifyTask.ExecuteAsync(identifyParameter)).Results;
var identifyFeatures = new List<IdentifyFeature>();
try
{
var dict = new Dictionary<int, IReadOnlyList<FieldInfo>>();
foreach (var identifyItem in identifyItems)
{
IReadOnlyList<FieldInfo> fields;
if(dict.ContainsKey(identifyItem.LayerID))
fields = dict[identifyItem.LayerID];
else
{
fields = await GetFieldInfo(dynamicLayer, identifyItem);
dict[identifyItem.LayerID] = fields;
}
var identifyFeature = ReplaceAliasWithFieldName(identifyItem, fields);
identifyFeatures.Add(identifyFeature);
}
}
catch (Exception ex)
{
throw;
}
return new KeyValuePair<Layer, IEnumerable<IdentifyFeature>>(layer, identifyFeatures);
}
if (layer is ArcGISTiledMapServiceLayer)
{
var tiledlayer = ((ArcGISTiledMapServiceLayer) layer);
if (!tiledlayer.ServiceInfo.Capabilities.Contains("Query"))
return new KeyValuePair<Layer, IEnumerable<IdentifyFeature>>(layer, null);
var identifyTask = new IdentifyTask(new Uri(tiledlayer.ServiceUri, UriKind.Absolute));
var resolution = controller.UnitsPerPixel;
var center = controller.Extent.GetCenter();
var extent = new Envelope(center.X - resolution * c_defaultTolerance, center.Y - resolution * c_defaultTolerance,
center.X + resolution * c_defaultTolerance, center.Y + resolution * c_defaultTolerance, controller.SpatialReference);
var identifyParameter = new IdentifyParameters(mapPoint, extent, c_defaultTolerance, c_defaultTolerance,
c_defaultTolerance, DisplayInformation.GetForCurrentView().LogicalDpi)
{
LayerOption = LayerOption.Visible,
TimeExtent = controller.TimeExtent,
};
var identifyItems = (await identifyTask.ExecuteAsync(identifyParameter)).Results;
var identifyFeatures = new List<IdentifyFeature>();
var dict = new Dictionary<int, IReadOnlyList<FieldInfo>>();
foreach (var identifyItem in identifyItems)
{
IReadOnlyList<FieldInfo> fields;
if (dict.ContainsKey(identifyItem.LayerID))
fields = dict[identifyItem.LayerID];
else
{
fields = await GetFieldInfo(tiledlayer, identifyItem);
dict[identifyItem.LayerID] = fields;
}
var identifyFeature = ReplaceAliasWithFieldName(identifyItem, fields);
identifyFeatures.Add(identifyFeature);
}
return new KeyValuePair<Layer, IEnumerable<IdentifyFeature>>(layer, identifyFeatures);
}
if (layer is FeatureLayer)
{
var featureLayer = ((FeatureLayer)layer);
var featureIds = await controller.FeatureLayerHitTestAsync(featureLayer, tapPoint, 1000);
IEnumerable<Feature> features = new List<Feature>();
if (featureIds != null && featureIds.Any())
features = await featureLayer.FeatureTable.QueryAsync(featureIds);
var schema = featureLayer.FeatureTable.Schema;
IList<IdentifyFeature> displayFeatures = new List<IdentifyFeature>();
foreach (var f in features)
{
var objectIDFieldName = schema.Fields.FirstOrDefault(x => x.Type == FieldType.Oid).Name;
var objectIDFieldValue = f.Attributes[objectIDFieldName].ToString();
IdentifyItem identifyItem = new IdentifyItem(-1, featureLayer.DisplayName, objectIDFieldName, objectIDFieldValue, f);
IdentifyFeature identifyFeature = new IdentifyFeature(identifyItem, schema.Fields);
displayFeatures.Add(identifyFeature);
}
return new KeyValuePair<Layer, IEnumerable<IdentifyFeature>>(layer, displayFeatures);
}
if (layer is GraphicsLayer)
{
var graphicsLayer = ((GraphicsLayer) layer);
var graphics = await controller.GraphicsLayerHitTestAsync(graphicsLayer, tapPoint, 1000);
return new KeyValuePair<Layer, IEnumerable<IdentifyFeature>>(layer, graphics.Select(f => new IdentifyFeature(new IdentifyItem(
-1,
layer.DisplayName,
"",
"",
f
))));
}
// Not supported or not implemented yet
return new KeyValuePair<Layer, IEnumerable<IdentifyFeature>>(layer,null);
}