Visual Basic (Declaration) | |
---|---|
Public Property OutSpatialReference As SpatialReference |
C# | |
---|---|
public SpatialReference OutSpatialReference {get; set;} |
The Query.OutSpatialReference Property determines the SpatialReference of the returned Graphics. Typically, you would set the Query.OutSpatialReference to the Map.SpatialReference if you want to display those returned Graphics in the Map. NOTE: it is not always true that the features in the feature service are in the same SpatialReference as the Map control; many times the data in the feature service are re-projected on-the-fly to match the Map.SpatialReference. Setting the Query.OutSpatialRefernce to that of the Map.SpatialReference saves the client application time from having to iterate through the Graphics in the returned FeatureSet and applying a custom project using a Method like ESRI.ArcGIS.Client.Tasks.GeometryService.ProjectAsync(IEnumerable<Graphic>,SpatialReference).
How to use:
Click the two buttons to see how changing the Query.OutSpatialReference yields different results. The feature returned for the QueryTask is the state of Maine.
The XAML code in this example is used in conjunction with the code-behind (C# or VB.NET) to demonstrate the functionality.
The following screen shot corresponds to the code example in this page.
XAML | Copy Code |
---|---|
<Grid x:Name="LayoutRoot" Background="White"> <!-- Manually change the Map.SpatialReference to WKID = 4269. Set the initial Map.Extent to be the area of Maine for that SpatialReference. --> <esri:Map Background="LightGray" Margin="12,238,538,12" Name="map1" Height="250" Width="250" HorizontalAlignment="Left" VerticalAlignment="Top"> <esri:Map.Extent > <esri:Envelope XMin="-71.80" YMin="42.56" XMax="-66.47" YMax="47.89"> <esri:Envelope.SpatialReference> <esri:SpatialReference WKID="4269"/> </esri:Envelope.SpatialReference> </esri:Envelope> </esri:Map.Extent> </esri:Map> <!-- Manually change the Map.SpatialReference to WKID = 53030. Set the initial Map.Extent to be the area of Maine for that SpatialReference. --> <esri:Map Background="LightBlue" Margin="276,238,274,12" Name="map2" Height="250" Width="250" HorizontalAlignment="Left" VerticalAlignment="Top"> <esri:Map.Extent > <esri:Envelope XMin="-8072381" YMin="5304258" XMax="-7314404" YMax="6062235"> <esri:Envelope.SpatialReference> <esri:SpatialReference WKID="102100"/> </esri:Envelope.SpatialReference> </esri:Envelope> </esri:Map.Extent> </esri:Map> <!-- Add buttons to perform the work showing how the changing the Query.OutSpatialReference can yield different results. Both buttons call the same click event handler. --> <Button Content="Query.OutSpatialReference = 4269" Height="23" HorizontalAlignment="Left" Margin="12,202,0,0" Name="Button_WKID4269" VerticalAlignment="Top" Width="250" Click="Button_Click"/> <Button Content="Query.OutSpatialReference = 102100" Height="23" HorizontalAlignment="Left" Margin="276,202,0,0" Name="Button_WKID102100" VerticalAlignment="Top" Width="250" Click="Button_Click"/> <!-- Provide the instructions on how to use the sample code. --> <TextBlock Height="138" HorizontalAlignment="Left" Name="TextBlock1" VerticalAlignment="Top" Width="590" TextWrapping="Wrap" Text="Click the two buttons to see how changing the Query.OutSpatialReference yields different results. The feature returned for the QueryTask is the state of Maine."/> </Grid> |
C# | Copy Code |
---|---|
private void Button_Click(object sender, System.Windows.RoutedEventArgs e) { // This function will execute when the user clicks either of the two buttons. The purpose is to // demonstrate various ways that a Query.OutSpatialRefernce yields different projected geometries. // Create a new QueryTask object. Set the Url to be a sample public FeatureLayer. ESRI.ArcGIS.Client.Tasks.QueryTask myQueryTask = new ESRI.ArcGIS.Client.Tasks.QueryTask(); myQueryTask.Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"; // Wire up the event handler for when the QueryTask completes. myQueryTask.ExecuteCompleted += myQueryTask_ExecuteCompleted; // Create a new Query object. ESRI.ArcGIS.Client.Tasks.Query myQuery = new ESRI.ArcGIS.Client.Tasks.Query(); // Specify the ObjectID value for the State of Maine. myQuery.ObjectIDs = new int[]{3}; // Return the Geometry to the QueryTask.ExecuteCompleted Event. myQuery.ReturnGeometry = true; // Get the button the user just clicked. Button theButton = (Button)sender; // Branch setting the Query.OutSpatialReference depending on which button the user clicked. Also pass the // correct MapContol as the UserToken object to the QueryTask.Execute Method. if (theButton.Name == "Button_WKID4269") { // Use Map1. myQuery.OutSpatialReference = map1.SpatialReference; myQueryTask.ExecuteAsync(myQuery, map1); } else if (theButton.Name == "Button_WKID102100") { // Use Map2. myQuery.OutSpatialReference = map2.SpatialReference; myQueryTask.ExecuteAsync(myQuery, map2); } } private void myQueryTask_ExecuteCompleted(object sender, ESRI.ArcGIS.Client.Tasks.QueryEventArgs e) { // Get the returned FeatureSet from the QueryTask. ESRI.ArcGIS.Client.Tasks.FeatureSet myFeatureSet = e.FeatureSet; // Create a new GraphicsLayer and set its ID. ESRI.ArcGIS.Client.GraphicsLayer myGraphicsLayer = new ESRI.ArcGIS.Client.GraphicsLayer(); myGraphicsLayer.ID = "TheGraphicsLayer"; // Create a SimpleFillSymbol to draw the graphics features added to the GraphicsLayer. ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol mySimpleFillSymbol = new ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol(); System.Windows.Media.SolidColorBrush mySolidColorBrush = new System.Windows.Media.SolidColorBrush(Colors.Red); mySolidColorBrush.Opacity = 0.5; mySimpleFillSymbol.Fill = mySolidColorBrush; // Ensure the we have a FeatureSet with data. if (myFeatureSet != null && myFeatureSet.Features.Count > 0) { // Iterate over each feature (which is a Graphic object) in the FeatureSet. foreach (ESRI.ArcGIS.Client.Graphic graphicFeature in myFeatureSet.Features) { if (graphicFeature.Geometry is ESRI.ArcGIS.Client.Geometry.Polygon) { // The Graphic returned has geometry values. // Attach the custom solid fill symbol to the Graphic. graphicFeature.Symbol = mySimpleFillSymbol as ESRI.ArcGIS.Client.Symbols.Symbol; // Add the Graphic to the GraphicsLayer myGraphicsLayer.Graphics.Add(graphicFeature); } } } // Get the Map Control from the UserState object. ESRI.ArcGIS.Client.Map theMapControl = (ESRI.ArcGIS.Client.Map) e.UserState; // Add the GraphicsLayer to the correct Map Control. It will automatically draw; there is not a need for a refresh. theMapControl.Layers.Add(myGraphicsLayer); } |
VB.NET | Copy Code |
---|---|
Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) ' This function will execute when the user clicks either of the two buttons. The purpose is to ' demonstrate various ways that a Query.OutSpatialReference yields different projected geometries. ' Create a new QueryTask object. Set the Url to be a sample public FeatureLayer. Dim myQueryTask As ESRI.ArcGIS.Client.Tasks.QueryTask = New ESRI.ArcGIS.Client.Tasks.QueryTask myQueryTask.Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5" ' Wire up the event handler for when the QueryTask completes. AddHandler myQueryTask.ExecuteCompleted, AddressOf myQueryTask_ExecuteCompleted ' Create a new Query object. Dim myQuery As ESRI.ArcGIS.Client.Tasks.Query = New ESRI.ArcGIS.Client.Tasks.Query ' Specify the ObjectID value for the State of Maine. myQuery.ObjectIDs = {3} ' Return the Geometry to the QueryTask.ExecuteCompleted Event. myQuery.ReturnGeometry = True ' Get the button the user just clicked. Dim theButton As Button = sender ' Branch setting the Query.OutSpatialReference depending on which button the user clicked. Also pass the ' correct MapContol as the UserToken object to the QueryTask.Execute Method. If theButton.Name = "Button_WKID4269" Then ' Use Map1. myQuery.OutSpatialReference = map1.SpatialReference myQueryTask.ExecuteAsync(myQuery, map1) ElseIf theButton.Name = "Button_WKID102100" Then ' Use Map2. myQuery.OutSpatialReference = map2.SpatialReference myQueryTask.ExecuteAsync(myQuery, map2) End If End Sub Private Sub myQueryTask_ExecuteCompleted(sender As Object, e As ESRI.ArcGIS.Client.Tasks.QueryEventArgs) ' Get the returned FeatureSet from the QueryTask. Dim myFeatureSet As ESRI.ArcGIS.Client.Tasks.FeatureSet = e.FeatureSet ' Create a new GraphicsLayer and set its ID. Dim myGraphicsLayer As ESRI.ArcGIS.Client.GraphicsLayer = New ESRI.ArcGIS.Client.GraphicsLayer myGraphicsLayer.ID = "TheGraphicsLayer" ' Create a SimpleFillSymbol to draw the graphics features added to the GraphicsLayer. Dim mySimpleFillSymbol As New ESRI.ArcGIS.Client.Symbols.SimpleFillSymbol Dim mySolidColorBrush As New System.Windows.Media.SolidColorBrush(Colors.Red) mySolidColorBrush.Opacity = 0.5 mySimpleFillSymbol.Fill = mySolidColorBrush ' Ensure the we have a FeatureSet with data. If myFeatureSet IsNot Nothing AndAlso myFeatureSet.Features.Count > 0 Then ' Iterate over each feature (which is a Graphic object) in the FeatureSet. For Each graphicFeature As ESRI.ArcGIS.Client.Graphic In myFeatureSet.Features If TypeOf graphicFeature.Geometry Is ESRI.ArcGIS.Client.Geometry.Polygon Then ' The Graphic returned has geometry values. ' Attach the custom solid fill symbol to the Graphic. graphicFeature.Symbol = TryCast(mySimpleFillSymbol, ESRI.ArcGIS.Client.Symbols.Symbol) ' Add the Graphic to the GraphicsLayer myGraphicsLayer.Graphics.Add(graphicFeature) End If Next graphicFeature End If ' Get the Map Control from the UserState object. Dim theMapControl As ESRI.ArcGIS.Client.Map = e.UserState ' Add the GraphicsLayer to the correct Map Control. It will automatically draw; there is not a need for a refresh. theMapControl.Layers.Add(myGraphicsLayer) End Sub |
Target Platforms: Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7