ArcGIS Runtime WPF SDK (3) 小试身手

本文介绍了如何使用WPF API创建地图应用,包括加载底图、要素图层,并实现点选查询功能。通过代码示例展示了如何设置要素选择的颜色、宽度及响应点击事件,同时还包含了弹出窗口显示查询结果的方法。

本节代码

在上一节中,了解了,WPF API的各个命名空间的作用,本节内容包括,创建一个地图,加载一个要素图层,并支持点选查询。

效果如下:
这里写图片描述

底图

var myMap = new Map(Basemap.CreateTopographic());

要素图层

// Create Uri for the feature service
Uri featureServiceUri = new Uri(            "http://sampleserver6.arcgisonline.com/arcgis/rest/services/DamageAssessment/FeatureServer/0");
// Initialize feature table using a url to feature server url
var featureTable = new ServiceFeatureTable(featureServiceUri);
// Initialize a new feature layer based on the feature table
_featureLayer = new FeatureLayer(featureTable);
await _featureLayer.LoadAsync();
// Check for the load status. If the layer is loaded then add it to map
if (_featureLayer.LoadStatus == LoadStatus.Loaded)
{
        // Add the feature layer to the map
        myMap.OperationalLayers.Add(_featureLayer);
}

支持点选

// Set the selection color for feature layer
_featureLayer.SelectionColor = Colors.Cyan;
// Set the selection width
_featureLayer.SelectionWidth = 3;
// Add tap event handler for mapview
MyMapView.GeoViewTapped += OnMapViewTapped;

 pivate async void OnMapViewTapped(object sender, GeoViewInputEventArgs e)
 {
     try
     {
         // Define the selection tolerance (half the marker symbol size so that any click on the symbol will select the feature)
         double tolerance = 14;

         // Convert the tolerance to map units
         double mapTolerance = tolerance * MyMapView.UnitsPerPixel;

         // Define the envelope around the tap location for selecting features
         var selectionEnvelope = new Envelope(e.Location.X - mapTolerance, e.Location.Y - mapTolerance, e.Location.X + mapTolerance,
             e.Location.Y + mapTolerance, MyMapView.Map.SpatialReference);

         // Define the query parameters for selecting features
         var queryParams = new QueryParameters();

         // Set the geometry to selection envelope for selection by geometry
         queryParams.Geometry = selectionEnvelope;

         // Select the features based on query parameters defined above
         await _featureLayer.SelectFeaturesAsync(queryParams, Esri.ArcGISRuntime.Mapping.SelectionMode.New);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Sample error", ex.ToString());
     }
 }

弹出窗

在xaml中加入

<Grid>
   <esri:MapView x:Name="MyMapView" />
    <Border Name="myPopup" Width="200"
                          Height="100"
                          Margin="0,0,100,200">
        <!--<Path x:Name="myPopupPath">
            <Path.Data>
                <PathGeometry x:Name="myPopupPathGeometry" Figures="{Binding FiguresPopupPathGeometry}"/>
            </Path.Data>
        </Path>-->
        <TextBlock Background="White" Foreground="Blue">Test</TextBlock>
    </Border>
</Grid>

在cs中加入相应代码,控制popup位置,以及显示内容

bool hasResult = false;
foreach(var r in _queryResult)
{
    hasResult = true;
    Feature f = r as Feature;
    string attribute = "";
    foreach(var a in f.Attributes)
    {
        attribute = attribute + a.ToString() + "\n";
    }

    MapPoint p = f.Geometry as MapPoint;

    Point sp = MyMapView.LocationToScreen(p);
    TextBlock popupText = new TextBlock();
    popupText.Background = Brushes.LightBlue;
    popupText.Foreground = Brushes.Blue;
    popupText.Text = attribute;
    myPopup.Child = popupText;
    myPopup.Width = 200;
    myPopup.Height = 100;
    var b = MyMapView.Margin;

    myPopup.Margin = new Thickness(sp.X, sp.Y, MyMapView.ActualWidth - 200 - sp.X, MyMapView.ActualHeight - 100 - sp.Y);
}

if(!hasResult)
{
    myPopup.Width = myPopup.Height = 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值