arcgis runtime100(十一、十二课时)

第十一、十二课时查询

Identify methods are asynchronous, so that the UI thread of your application is not blocked waiting for results.

识别方法都是异步方法,不会阻塞UI进程

识别元素

在一个元素图层里查询元素

  1. 得到点击点
MyMapView.GeoViewTapped += async (s, e) =>
{
    // get the tap location in screen units
    System.Windows.Point tapScreenPoint = e.Position;


    // ... code here to identify using the tap point ...
};
  1. 调取识别方法
    • 选择一个图层
    • 选择一个屏幕识别半径
    • 选择是否包含弹出窗口
    • 选择返回数量
var layer = MyMapView.Map.OperationalLayers[0];
var pixelTolerance = 20;
var returnPopupsOnly = false;
var maxResults = 5;
IdentifyLayerResult idResults = await MyMapView.IdentifyLayerAsync(layer, tapScreenPoint, pixelTolerance, returnPopupsOnly, maxResults);
  1. 迭代结果
FeatureLayer idLayer = idResults.LayerContent as FeatureLayer;

foreach (GeoElement idElement in idResults.GeoElements)
{
    Feature idFeature = idElement as Feature;

    idLayer.SelectFeature(idFeature);
}

在所有元素图层中查找元素

// get the tap location in screen units
System.Windows.Point tapScreenPoint = e.Position;

//查看此处不同
var pixelTolerance = 20;
var returnPopupsOnly = false;
var maxLayerResults = 5;


// 此处是关键
IReadOnlyList<IdentifyLayerResult> idLayerResults = await MyMapView.IdentifyLayersAsync(tapScreenPoint, pixelTolerance, returnPopupsOnly, maxLayerResults);


// iterate the results for each layer
foreach (IdentifyLayerResult idResults in idLayerResults)
{
    // get the layer identified and cast it to FeatureLayer
    FeatureLayer idLayer = idResults.LayerContent as FeatureLayer;


    // iterate each identified GeoElement in the results for this layer
    foreach (GeoElement idElement in idResults.GeoElements)
    {
        // cast the result GeoElement to Feature
        Feature idFeature = idElement as Feature;


        // select this feature in the feature layer
        idLayer.SelectFeature(idFeature);
    }
}

只识别最顶层项

// get the tap location in screen units
System.Windows.Point tapScreenPoint = e.Position;


var pixelTolerance = 20;
var returnPopupsOnly = false;


// identify the topmost feature for all layers, passing in the tap point, tolerance, and types to return
IReadOnlyList<IdentifyLayerResult> idLayerResults = await MyMapView.IdentifyLayersAsync(tapScreenPoint, pixelTolerance, returnPopupsOnly);


// iterate the results for each layer
foreach (IdentifyLayerResult idResults in idLayerResults)
{
    // get the layer identified and cast it to FeatureLayer
    FeatureLayer idLayer = idResults.LayerContent as FeatureLayer;


    // see if a result was found for this layer (will be 0 or 1 result for each layer)
    if (idResults.GeoElements.Count == 1)
    {
        // cast the result GeoElement to Feature
        Feature idFeature = idResults.GeoElements[0] as Feature;


        // select this feature in the feature layer
        idLayer.SelectFeature(idFeature);
    }
}

在image layer上做识别

private void ProcessIdentifyResults(IReadOnlyList<IdentifyLayerResult> idLayerResults)
{
    // iterate the results for each layer
    foreach (IdentifyLayerResult idResults in idLayerResults)
    {
        // get the layer identified and try to cast it to FeatureLayer
        FeatureLayer featLayer = idResults.LayerContent as FeatureLayer;


        // process feature layer results
        if (featLayer != null)
        {
            // iterate each identified GeoElement in the results for this layer
            foreach (GeoElement idElement in idResults.GeoElements)
            {
                // cast the result GeoElement to Feature
                Feature idFeature = idElement as Feature;


                // select this feature in the feature layer
                featLayer.SelectFeature(idFeature);
            }
        }


        // if not a feature layer, check for map image layer
        if (idResults.LayerContent is ArcGISMapImageLayer || idResults.LayerContent is ArcGISMapImageSublayer)
        {
            // iterate each identified GeoElement in the results
            foreach (GeoElement idElement in idResults.GeoElements)
            {
                // cast the result GeoElement to Feature
                Feature idFeature = idElement as Feature;


                // can get the geometry and attributes, but FeatureTable will be null
                Geometry featureShape = idFeature.Geometry;
                IDictionary<string, object> featureAttrs = idFeature.Attributes;


                // ... code to display the GeoElement attributes here ...


            }


            // see if there are sublayer results, call this function again and pass them in
            ProcessIdentifyResults(idResults.SublayerResults);
        }
    }                      
}

返回弹出框

// identify all layers in the MapView, passing the tap point, tolerance, types to return, and max results
IReadOnlyList<IdentifyLayerResult> idLayerResults = await MyMapView.IdentifyLayersAsync(tapScreenPoint, pixelTolerance, returnPopupsOnly, maxLayerResults);
 if (idLayerResults.Count > 0)
{
    // get the first identify result
    IdentifyLayerResult firstIdLayerResult = idLayerResults.FirstOrDefault();


    if (firstIdLayerResult.Popups.Count > 0)
    {
        // get the first popup from the result
        Popup firstPopup = firstIdLayerResult.Popups.FirstOrDefault();


        // ... code here to show popup ...
    }
}

在WMS layer上做识别

// Perform the identify operation on the WMS layer
IdentifyLayerResult myIdentifyResult = await MyMapView.IdentifyLayerAsync(myWmsLayer, e.Position, 20, false);

// Return if there's no feature to show
if (myIdentifyResult.GeoElements.Count() < 1) { return; }

// Retrieve the identified feature, which is always a WmsFeature for WMS layers
WmsFeature identifiedFeature = (WmsFeature)myIdentifyResult.GeoElements[0];

// WMS图层只有HTML属性,并且没有geometry属性
string htmlContent = identifiedFeature.Attributes["HTML"].ToString();

识别graphics

// identify all graphics overlays in the MapView, passing the tap point, tolerance, types to return, and max results
IReadOnlyList<IdentifyGraphicsOverlayResult> idGraphicOverlayResults = await MyMapView.IdentifyGraphicsOverlaysAsync(tapScreenPoint, pixelTolerance, returnPopupsOnly, maxLayerResults);


// iterate each graphics overlay
foreach(IdentifyGraphicsOverlayResult idGraphicResult in idGraphicOverlayResults)
{
    // iterate all graphics in the overlay and select them
    foreach(Graphic g in idGraphicResult.Graphics)
    {
        g.IsSelected = true;
    }
}

识别关系元素

示例 data/FeatureLayerQuery

示例data中的三种加载方式

视频资料

posted @ 2018-03-14 08:53  小狮子头  阅读(455)  评论(0编辑  收藏  举报