
照例让我们以源码开始
<GrID x:name="LayoutRoot" Background="White">
<GrID.Resources>
<symbol:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="#01FFFFFF" borderBrush="#88000000"
borderThickness="2" />
<symbol:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" color="Red" Size="6" Style="Diamond" />
</GrID.Resources>
<esri:Map x:name="MyMap" WrapAround="True" Extent="-15000000,2000000,-7000000,8000000">
<esri:ArcGISTiledMapServiceLayer ID="BaseMapLayer"
Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
<esri:Graphicslayer ID="CitIEsGraphicslayer">
<esri:Graphicslayer.MapTip>
<border borderBrush="DarkGray" CornerRadius="13" borderThickness="1" margin="0,15,15">
<border.Effect>
<DropShadowEffect ShadowDepth="10" BlurRadius="14" Direction="300" />
</border.Effect>
<border CornerRadius="10" Background="#DDFFEEEE" borderThickness="5" borderBrush="#77FF0000">
<StackPanel OrIEntation="Vertical" HorizontalAlignment="Center" margin="10">
<StackPanel OrIEntation="Horizontal">
<TextBlock Text="City name: " FontWeight="Bold" Foreground="#FF0F274E" FontSize="10" VerticalAlignment="Center"/>
<TextBlock Text="{Binding [CITY_name]}" HorizontalAlignment="left" VerticalAlignment="Center" />
</StackPanel>
<StackPanel OrIEntation="Horizontal">
<TextBlock Text="Population: " FontWeight="Bold" Foreground="#FF0F274E" FontSize="10" VerticalAlignment="Center" />
<TextBlock Text="{Binding [POP1990]}" HorizontalAlignment="left" VerticalAlignment="Center" />
</StackPanel>
</StackPanel>
</border>
</border>
</esri:Graphicslayer.MapTip>
</esri:Graphicslayer>
</esri:Map>
</GrID>
注意前端引入 xmlns:symbol="clr-namespace:ESRI.ArcGIS.ClIEnt.Symbols;assembly=ESRI.ArcGIS.ClIEnt"
后台:
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
MyMap.PropertyChanged += MyMap_PropertyChanged;
}
voID MyMap_PropertyChanged(object sender,System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.Propertyname == "SpatialReference")
{
CitIEsGraphicslayerLoad();
MyMap.PropertyChanged -= MyMap_PropertyChanged;
}
}
private voID CitIEsGraphicslayerLoad()
{
ESRI.ArcGIS.ClIEnt.Tasks.query query = new ESRI.ArcGIS.ClIEnt.Tasks.query()
{
Geometry = new ESRI.ArcGIS.ClIEnt.Geometry.Envelope(-180,90),
ReturnGeometry = true,
OutSpatialReference = MyMap.SpatialReference
};
query.OutFIElds.Add("*");
query.Where = "POP1990 > 100000";
queryTask queryTask = new queryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0");
queryTask.ExecuteCompleted += CitIEsGraphicslayerqueryTask_ExecuteCompleted;
queryTask.ExecuteAsync(query);
}
voID CitIEsGraphicslayerqueryTask_ExecuteCompleted(object sender,queryEventArgs queryArgs)
{
if (queryArgs.Featureset == null)
return;
Featureset resultFeatureset = queryArgs.Featureset;
ESRI.ArcGIS.ClIEnt.Graphicslayer graphicslayer =
MyMap.Layers["CitIEsGraphicslayer"] as ESRI.ArcGIS.ClIEnt.Graphicslayer;
if (resultFeatureset != null && resultFeatureset.Features.Count > 0)
{
foreach (ESRI.ArcGIS.ClIEnt.Graphic graphicFeature in resultFeatureset.Features)
{
graphicFeature.Symbol = LayoutRoot.Resources["DefaultMarkerSymbol"] as ESRI.ArcGIS.ClIEnt.Symbols.Symbol;
graphicslayer.Graphics.Add(graphicFeature);
}
}
}
}
运行看看效果,然后我们再研究queryTask的神秘。
queryTask是一个进行空间和属性查询的功能类,它是针对某个地图服务的某个子图层进行查询(这个是与后面介绍FindTask的差别所在)。下面分别看看属性查询和空间查询的实例,其实MapTips的实现也是利用queryTask查询属性。不妨再看一个查询属性应用:
<GrID x:name="LayoutRoot" Background="White">
<Canvas x:name="ResultsdisplayCanvas" HorizontalAlignment="Center" VerticalAlignment="top" WIDth="547" Height="200" margin="0,30,0" >
<Rectangle stroke="Gray" RadiusX="10" RadiusY="10" Fill="#77919191" Canvas.left="0" Canvas.top="0" WIDth="547" Height="200" >
<Rectangle.Effect>
<DropShadowEffect/>
</Rectangle.Effect>
</Rectangle>
<Rectangle Fill="#FFFFFFFF" stroke="DarkGray" RadiusX="5" RadiusY="5" Canvas.left="10" Canvas.top="20" WIDth="530" Height="170" />
<TextBlock x:name="DatadisplayTitle" Text="Using query tasks without maps" Foreground="White" FontSize="9" Canvas.left="10" Canvas.top="4" FontWeight="Bold" />
<StackPanel OrIEntation="Horizontal" margin="5,0" Canvas.top="25" >
<TextBlock Text="US State name contains:" margin="10,0" VerticalAlignment="Center"/>
<TextBox x:name="StatenameTextBox" Text="New" Height="23" HorizontalAlignment="left" VerticalAlignment="Center" WIDth="125" textwrapPing="nowrap"
margin="10,10,0" FontSize="12" Background="White" AcceptsReturn="False" />
<button Content="Do query" WIDth="75" VerticalAlignment="Center" HorizontalAlignment="Right" Click="querybutton_Click" margin="0,0" Cursor="Hand" />
</StackPanel>
<slData:DataGrID x:name="queryDetailsDataGrID" autoGenerateColumns="False" headersVisibility="Column" Background="White"
IsReadonly="True" Canvas.left="10" Canvas.top="50" Height="140" WIDth="530"
HorizontalScrollbarVisibility="HIDden">
<slData:DataGrID.Columns>
<slData:DataGrIDTextColumn WIDth="85" Binding="{Binding Attributes[STATE_name]}" header="State name"/>
<slData:DataGrIDTextColumn WIDth="110" Binding="{Binding Attributes[SUB_REGION] }" header="Region"/>
<slData:DataGrIDTextColumn WIDth="45" Binding="{Binding Attributes[STATE_FIPS] }" header="FIPS"/>
<slData:DataGrIDTextColumn WIDth="85" Binding="{Binding Attributes[STATE_ABBR] }" header="Abbreviation"/>
<slData:DataGrIDTextColumn WIDth="103" Binding="{Binding Attributes[POP2000] }" header="Population 2000"/>
<slData:DataGrIDTextColumn WIDth="102" Binding="{Binding Attributes[POP2007] }" header="Population 2007"/>
</slData:DataGrID.Columns>
</slData:DataGrID>
</Canvas>
</GrID>
public partial class queryWithoutMap : UserControl
{
public queryWithoutMap()
{
InitializeComponent();
}
voID querybutton_Click(object sender,RoutedEventArgs e)
{
queryTask queryTask =
new queryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");
queryTask.ExecuteCompleted += queryTask_ExecuteCompleted;
queryTask.Failed += queryTask_Failed;
ESRI.ArcGIS.ClIEnt.Tasks.query query = new ESRI.ArcGIS.ClIEnt.Tasks.query();
query.Text = StatenameTextBox.Text;
query.OutFIElds.Add("*");
queryTask.ExecuteAsync(query);
}
voID queryTask_ExecuteCompleted(object sender,ESRI.ArcGIS.ClIEnt.Tasks.queryEventArgs args)
{
Featureset featureset = args.Featureset;
if (featureset != null && featureset.Features.Count > 0)
queryDetailsDataGrID.ItemsSource = featureset.Features;
else
MessageBox.Show("No features returned from query");
}
private voID queryTask_Failed(object sender,TaskFailedEventArgs args)
{
MessageBox.Show("query execute error: " + args.Error);
}
}
这段代码中注意 query.Text = StatenameTextBox.Text;和 query.OutFIElds.Add("*")。query.Text值对应Url中的子图层的显示字段,也就是在Url中图层的显示字段中模糊查找query.Text。直接访问Uri会看到display FIEld: STATE_name,这样就等同另种写法query.Where = "STATE_name like '%" + StatenameTextBox.Text + "%'";
query.OutFIElds.Add("*")表示返回所有属性字段。
下面看一个queryTask查询空间的实例:
<GrID x:name="LayoutRoot" Background="White">
<GrID.Resources>
<Style x:Key="MyCustomrow" targettype="slData:DataGrIDRow">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate targettype="slData:DataGrIDRow">
<slPrimitives:DataGrIDFroZenGrID name="Root">
<visualstatemanager.VisualStateGroups>
<VisualStateGroup x:name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0" />
</VisualStateGroup.Transitions>
<VisualState x:name="normal" >
<Storyboard>
<DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/>
<colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">
<SplinecolorKeyFrame KeyTime="0" Value="#ebf4fa"/>
</colorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:name="normal AlternatingRow">
<Storyboard>
<DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="0"/>
<colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">
<SplinecolorKeyFrame KeyTime="0" Value="#bbd9ee"/>
</colorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:name="MouSEOver">
<Storyboard>
<DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/>
<colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">
<SplinecolorKeyFrame KeyTime="0" Value="#8800FFFF"/>
</colorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:name="normal Selected">
<Storyboard>
<DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
<colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">
<SplinecolorKeyFrame KeyTime="0" Value="#8800FFFF"/>
</colorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:name="MouSEOver Selected">
<Storyboard>
<DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
<colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">
<SplinecolorKeyFrame KeyTime="0" Value="#8800FFFF"/>
</colorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:name="Unfocused Selected">
<Storyboard>
<DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
<colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">
<SplinecolorKeyFrame KeyTime="0" Value="#8800FFFF"/>
</colorAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</visualstatemanager.VisualStateGroups>
<GrID.RowDeFinitions>
<RowDeFinition Height="*"/>
<RowDeFinition Height="auto"/>
<RowDeFinition Height="auto"/>
</GrID.RowDeFinitions>
<GrID.ColumnDeFinitions>
<ColumnDeFinition WIDth="auto" />
<ColumnDeFinition WIDth="*" />
</GrID.ColumnDeFinitions>
<GrID.Resources>
<Storyboard x:Key="DetailsVisibleTransition">
<DoubleAnimation Storyboard.Targetname="DetailsPresenter" Storyboard.TargetProperty="ContentHeight"
Duration="00:00:0.1" />
</Storyboard>
</GrID.Resources>
<Rectangle x:name="BackgroundRectangle" GrID.rowspan="2" GrID.ColumnSpan="2" Opacity="0"
Fill="#FFFFCC00"/>
<slPrimitives:DataGrIDRowheader GrID.rowspan="3" name="Rowheader"
slPrimitives:DataGrIDFroZenGrID.IsFroZen="True" />
<slPrimitives:DataGrIDCellsPresenter GrID.Column="1" name="CellsPresenter"
slPrimitives:DataGrIDFroZenGrID.IsFroZen="True" />
<slPrimitives:DataGrIDDetailsPresenter GrID.Row="1" GrID.Column="1" name="DetailsPresenter" />
<Rectangle GrID.Row="2" GrID.Column="1" name="BottomGrIDline" HorizontalAlignment="Stretch"
Height="1" />
</slPrimitives:DataGrIDFroZenGrID>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<symbol:SimpleMarkerSymbol x:Key="DefaultMarkerSymbol" color="Red" Size="12" Style="Circle" />
<symbol:Simplelinesymbol x:Key="Defaultlinesymbol" color="Red" WIDth="4" />
<symbol:FillSymbol x:Key="DefaultFillSymbol" >
<symbol:FillSymbol.ControlTemplate>
<ControlTemplate>
<Path x:name="Element" IsHitTestVisible="False" Fill="#66FF0000"
stroke="Red" strokeThickness="1"/>
</ControlTemplate>
</symbol:FillSymbol.ControlTemplate>
</symbol:FillSymbol>
<symbol:FillSymbol x:Key="ResultsFillSymbol">
<symbol:FillSymbol.ControlTemplate>
<ControlTemplate x:name="CustompolygonTemplate">
<GrID>
<visualstatemanager.VisualStateGroups>
<VisualStateGroup x:name="CommonStates">
<VisualState x:name="normal">
<Storyboard>
<colorAnimation Storyboard.Targetname="Element"
Storyboard.TargetProperty="(Fill).(color)"
To="#880000FF" Duration="0:0:0.1" />
</Storyboard>
</VisualState>
<VisualState x:name="MouSEOver">
<Storyboard>
<colorAnimation Storyboard.Targetname="Element"
Storyboard.TargetProperty="(Fill).(color)"
To="#8800FFFF" Duration="0:0:0.1" />
</Storyboard>
</VisualState>
<VisualState x:name="Selected">
<Storyboard>
<colorAnimation Storyboard.Targetname="Element"
Storyboard.TargetProperty="(Fill).(color)"
To="#8800FFFF" Duration="0:0:0.1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</visualstatemanager.VisualStateGroups>
<Path x:name="Element" stroke="Blue" Fill="#880000FF"
strokeStartlineCap="Round" strokeThickness="2"
strokelineJoin="Round" strokeEndlineCap="Round" />
</GrID>
</ControlTemplate>
</symbol:FillSymbol.ControlTemplate>
</symbol:FillSymbol>
</GrID.Resources>
<esri:Map x:name="MyMap" WrapAround="True" Extent="-15000000,8000000">
<esri:ArcGISTiledMapServiceLayer ID="StreetMapLayer"
Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
<esri:Graphicslayer ID="MySelectionGraphicslayer" />
</esri:Map>
</GrID>
注意引入xmlns:slPrimitives="clr-namespace:System.windows.Controls.Primitives;assembly=System.windows.Controls.Data"
后台:
public partial class MainPage : UserControl
{
private Draw MyDrawSurface;
public MainPage()
{
InitializeComponent();
MyDrawSurface = new Draw(MyMap)
{
linesymbol = LayoutRoot.Resources["Defaultlinesymbol"] as Simplelinesymbol,
FillSymbol = LayoutRoot.Resources["DefaultFillSymbol"] as FillSymbol
};
MyDrawSurface.DrawComplete += MyDrawSurface_DrawComplete;
MyDrawSurface.DrawMode = DrawMode.Point;
MyDrawSurface.IsEnabled = true;
}
private voID MyDrawSurface_DrawComplete(object sender,ESRI.ArcGIS.ClIEnt.DrawEventArgs args)
{
Graphicslayer selectionGraphicslayer = MyMap.Layers["MySelectionGraphicslayer"] as Graphicslayer;
selectionGraphicslayer.Cleargraphics();
queryTask queryTask = new queryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");
queryTask.ExecuteCompleted += queryTask_ExecuteCompleted;
queryTask.Failed += queryTask_Failed;
// Bind data grID to query results
Binding resultFeaturesBinding = new Binding("LastResult.Features");
resultFeaturesBinding.source = queryTask;
query query = new ESRI.ArcGIS.ClIEnt.Tasks.query();
// Specify fIElds to return from query
query.OutFIElds.AddRange(new string[] { "STATE_name","SUB_REGION","STATE_FIPS","STATE_ABBR","POP2000","POP2007" });
query.Geometry = args.Geometry;
// Return geometry with result features
query.ReturnGeometry = true;
query.OutSpatialReference = MyMap.SpatialReference;
queryTask.ExecuteAsync(query);
}
private voID queryTask_ExecuteCompleted(object sender,ESRI.ArcGIS.ClIEnt.Tasks.queryEventArgs args)
{
Featureset featureset = args.Featureset;
if (featureset == null || featureset.Features.Count < 1)
{
MessageBox.Show("No features retured from query");
return;
}
Graphicslayer graphicslayer = MyMap.Layers["MySelectionGraphicslayer"] as Graphicslayer;
if (featureset != null && featureset.Features.Count > 0)
{
foreach (Graphic feature in featureset.Features)
{
feature.Symbol = LayoutRoot.Resources["ResultsFillSymbol"] as FillSymbol;
graphicslayer.Graphics.Insert(0,feature);
}
}
MyDrawSurface.IsEnabled = true;
//MyDrawSurface.IsEnabled = (MyDrawSurface.DrawMode != DrawMode.None);
}
private voID queryTask_Failed(object sender,TaskFailedEventArgs args)
{
MessageBox.Show("query Failed: " + args.Error);
}
private voID queryDetailsDataGrID_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
foreach (Graphic g in e.AddedItems)
g.Select();
foreach (Graphic g in e.RemovedItems)
g.UnSelect();
}
}
运行,在地图上单击。
这个实例相信大家先运行 *** 作之后,再简单看看代码就会明白,在这我不解释,另外如果我们再往深处想一下,可以利用这点进行缓冲区的查询。
最后总之,大家记住进行空间和属性查询就是queryTask的主要应用。后面我们再介绍FindTask和IDentifyTask两个常用的功能类。
总结以上是内存溢出为你收集整理的arcgis api for silverlight开发系列之四:实现地图提示MapTips及QueryTask揭秘全部内容,希望文章能够帮你解决arcgis api for silverlight开发系列之四:实现地图提示MapTips及QueryTask揭秘所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)