How about just declaring one FeatureLayer and pass the url of your concerned layer (http://YourServer/ArcGIS/Rest/Services/ServiceName/MapSerer/layerIndex )to its constructor? Once it becomes the FeatureLayer, you can get its full extent.
<Grid x:Name="LayoutRoot" Background="White"> <Grid.Resources> <DataTemplate x:Key="ListBoxItem"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding ID}" Margin="5,0" /> <TextBlock Text="{Binding Name}" Grid.Column="1" /> </Grid> </DataTemplate> </Grid.Resources> <!--Map Control--> <esri:Map x:Name="esriMap"> <!--Base Layer--> <esri:ArcGISTiledMapServiceLayer ID="baseLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"/> <!--Dynamic Layer--> <esri:ArcGISDynamicMapServiceLayer ID="dynamicLayer" Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/WaterTemplate/WaterDistributionAdministrativeReport/MapServer" Initialized="ArcGISDynamicMapServiceLayer_Initialized"/> </esri:Map> <ListBox x:Name="SubLayersListBox" ItemTemplate="{StaticResource ListBoxItem}" SelectionChanged="SubLayersListBox_SelectionChanged" VerticalAlignment="Top" HorizontalAlignment="Right" /> </Grid>
ArcGISDynamicMapServiceLayer dynamicLayer; WebClient wc; SpatialReference WEB_MERCATOR = new SpatialReference(102100); SpatialReference GEOGRAPHIC = new SpatialReference(4326); WebMercator projection = new WebMercator(); public MainPage() { InitializeComponent(); wc = new WebClient(); wc.DownloadStringCompleted += wc_DownloadStringCompleted; } private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e) { dynamicLayer = esriMap.Layers["dynamicLayer"] as ArcGISDynamicMapServiceLayer; List<LayerInfo> sublayers = new List<LayerInfo>(); foreach (LayerInfo ld in dynamicLayer.Layers) { sublayers.Add(ld); } SubLayersListBox.ItemsSource = sublayers; } private void SubLayersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (SubLayersListBox.SelectedIndex < 0) return; LayerInfo sublayerMetaData = SubLayersListBox.SelectedItem as LayerInfo; if (sublayerMetaData != null) { Uri uri = new Uri(string.Format("{0}/{1}?f=json",dynamicLayer.Url,sublayerMetaData.ID)); if (wc.IsBusy) wc.CancelAsync(); if(!wc.IsBusy) wc.DownloadStringAsync(uri); } } void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) { if (e.Error != null) { MessageBox.Show(e.Error.Message); return; } JsonValue jValue = JsonObject.Parse(e.Result); if (jValue.ContainsKey("extent")) { string sExtent = jValue["extent"].ToString(); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sExtent))) { DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Envelope)); Envelope extent = ds.ReadObject(ms) as Envelope; if (!extent.SpatialReference.Equals(WEB_MERCATOR)) { if (extent.SpatialReference.Equals(GEOGRAPHIC)) { esriMap.ZoomTo(projection.FromGeographic(extent)); } else { MessageBox.Show("Need to project spatial reference."); } } else esriMap.ZoomTo(extent); }; } }
using System; using System.Collections.Generic; using System.IO; using System.Json; using System.Net; using System.Runtime.Serialization.Json; using System.Text; using System.Windows; using System.Windows.Controls; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Geometry; using ESRI.ArcGIS.Client.Projection;
I have to clarify what I said in this thread because it could be confusing.If your data are dynamic either because the data are loaded by other applications or because you are filtering at the client side (using layerdefs), it's not possible to easily know the extent at the client side (this was the initial subject of this thread).Now if your data are static and that you can rely on the extent provided by the service, it's possible to get the extent of layer and sublayers at the client side:- for a layer, there is a 'FullExtent' property- for a sublayer, you can deserialize the info provided by the sublayer rest resource. For example, look at the extent info provided by this sublayer.
function zoomToLayer(id) { //first, try the layer.fullExtent property var layer = map.getLayer(id); if (layer != null) { map.setExtent(layer.fullExtent); } else { //the layer is a sublayer, so we must dig deeper dojo.forEach(map.layerIds, function(layerId) { layer = map.getLayer(layerId); dojo.forEach(layer.layerInfos, function(layerInfo) { if (layerInfo.name == id) { //now we have a match, so make an AJAX request to the server to get the //json description of this sublayer, which contains the extent //so f'ing dumb that the LayerInfo object doesn't wrap this for us... var args = { url: "/arcgis/rest/services/Foo/Bar/MapServer/"+layerInfo.id, handleAs: "json", content: {f:"pjson"}, load:function(data) { var extent = data.extent; var sr = data.extent.spatialReference; map.setExtent(new esri.geometry.Extent(extent.xmin,extent.ymin,extent.xmax,extent.ymax,sr)); } }; var deferred = dojo.xhrGet(args); } }); }); } }
i also need to get into the sublayer of the service regardless of the effort needed .....
Dominique! Thank you for the confirmation...
I don't think it's possible if you are using MapServiceLayer.You could either develop your own web service to calculate the extent or load the sublayer as FeatureLayer instead of MapServiceLayer (but there is a big cost specially if the only goal is to get the extent).
So I am interesting in functionality on how to zoom to particular sublayer in the service, not the service itself... Is it possible?
Is this what you need or do you need?[HTML] <!--Map Control--> <esri:Map x:Name="esriMap"> <!--Base Layer--> <esri:ArcGISTiledMapServiceLayer ID="baseLayer" Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer"/> <!--Dynamic Layer--> <esri:ArcGISDynamicMapServiceLayer ID="dynamicLayer" Url="..." Initialized="ArcGISDynamicMapServiceLayer_Initialized"/> </esri:Map>[/HTML] private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e) { ArcGISDynamicMapServiceLayer dynamicLayer = esriMap.Layers["dynamicLayer"] as ArcGISDynamicMapServiceLayer; esriMap.Extent = dynamicLayer.FullExtent; }
private void ArcGISDynamicMapServiceLayer_Initialized(object sender, EventArgs e) { ArcGISDynamicMapServiceLayer dynamicLayer = esriMap.Layers["dynamicLayer"] as ArcGISDynamicMapServiceLayer; esriMap.Extent = dynamicLayer.FullExtent; }
What about this:Map.Extent = yourLayer.FullExtent;?
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.