Map.Layers.Clear(); collLayers.Remove(collLayers["BaseLayer"]);//layers collection Map.UpdateLayout(); lyr.ID = "BaseLayer"; Map = new ESRI.ArcGIS.Client.Map() { Extent = lyr.FullExtent, MaximumResolution=((TiledMapServiceLayer)lyr).TileInfo.Lods[0].Resolution , MinimumResolution = ((TiledMapServiceLayer)lyr).TileInfo.Lods[((TiledMapServiceLayer)lyr).TileInfo.Lods.Length-1].Resolution }; Map.Layers.Add(lyr);// add new base layer first foreach (Layer l in collLayers) // restore other layers { Map.Layers.Add(l); } Map.UpdateLayout();
Map = new ESRI.ArcGIS.Client.Map() { Extent = lyr.FullExtent, Visibility=Visibility.Visible, MaximumResolution=((TiledMapServiceLayer)lyr).TileInfo.Lods[0].Resolution , MinimumResolution = ((TiledMapServiceLayer)lyr).TileInfo.Lods[((TiledMapServiceLayer)lyr).TileInfo.Lods.Length-1].Resolution };
private void mnuBaseMap_Click(object sender, RoutedEventArgs e) { Layer newLyr = null; int WKID = -1; collLayers = new LayerCollection(); foreach (Layer l in Map.Layers) { collLayers.Add(l); } switch (((Button)sender).Name) { case "mnuESRIWorld": newLyr = new ESRI.ArcGIS.Client.ArcGISTiledMapServiceLayer() { Url = "http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" }; WKID = 102100; break; } if (newLyr != null) { newLyr.Initialized += new EventHandler<EventArgs>(newLyr_Initialized); newLyr.Initialize(); } } void newLyr_Initialized(object sender, EventArgs e) { Layer newLayer = (Layer)sender; int WKID = 0; if (newLayer.SpatialReference != null) WKID = newLayer.SpatialReference.WKID; ChangeBaseLayer(newLayer, WKID); Map.UpdateLayout(); } void ChangeBaseLayer(Layer lyr, int newWKID) { int baseIdx = Map.Layers.IndexOf(Map.Layers["BaseLayer"]); if (Map.SpatialReference.WKID == newWKID) { //skip } else //recreate map { Map.Layers.Clear(); collLayers.Remove(collLayers["BaseLayer"]); collLayers.Remove(collLayers["DummyMercator"]); Map.UpdateLayout(); lyr.ID = "BaseLayer"; ESRI.ArcGIS.Client.Map newMap = new ESRI.ArcGIS.Client.Map() { Extent = lyr.FullExtent, Visibility=Visibility.Visible, MaximumResolution=((TiledMapServiceLayer)lyr).TileInfo.Lods[0].Resolution , MinimumResolution = ((TiledMapServiceLayer)lyr).TileInfo.Lods[((TiledMapServiceLayer)lyr).TileInfo.Lods.Length-1].Resolution }; this.Map = newMap; Map.Layers.Add(lyr); foreach (Layer l in collLayers) //restore other layers { Map.Layers.Add(l); } Map.UpdateLayout(); } }
xmlns:esri="http://schemas.esri.com/arcgis/client/2009"> <Grid x:Name="LayoutRoot" Background="White"> <esri:Map x:Name="MyMap" Loaded="MyMap_Loaded"> <esri:ArcGISTiledMapServiceLayer ID="BaseLayer" Visible="True" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer" /> <esri:ArcGISDynamicMapServiceLayer ID="MyLayer" Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer" /> </esri:Map> <Grid HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10" > <Rectangle Fill="#77919191" Stroke="Gray" RadiusX="10" RadiusY="10" Margin="0" > <Rectangle.Effect> <DropShadowEffect/> </Rectangle.Effect> </Rectangle> <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Background="Transparent" Margin="10"> <RadioButton x:Name="StreetsRadioButton" Tag="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer" IsChecked="true" Margin="5,0,0,0" Foreground="White" GroupName="Layers" Content="4326" Click="RadioButton_Click"/> <RadioButton x:Name="TopoRadioButton" Tag="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" Margin="5,0,0,0" Foreground="White" GroupName="Layers" Content="102100" Click="RadioButton_Click"/> </StackPanel> </Grid> </Grid>
private void RadioButton_Click(object sender, RoutedEventArgs e) { LayerCollection otherLayers = new LayerCollection(); Map map = this.LayoutRoot.Children[0] as Map; foreach(var layer in map.Layers) if(!string.Equals(layer.ID, "BaseLayer", StringComparison.InvariantCultureIgnoreCase)) otherLayers.Add(layer); map.Layers.Clear(); ArcGISTiledMapServiceLayer arcgisLayer = new ArcGISTiledMapServiceLayer() { ID="BaseLayer", Url = ((RadioButton)sender).Tag as string }; Map newMap = new Map(); newMap.Layers.Add(arcgisLayer); foreach (var layer in otherLayers) newMap.Layers.Add(layer); this.LayoutRoot.Children[0] = newMap; } private void MyMap_Loaded(object sender, RoutedEventArgs e) { MyMap.Focus(); }
LayerCollection otherLayers = new LayerCollection(); Map map = this.LayoutRoot.Children[0] as Map; map.Loaded -= MyMap_Loaded; map.ExtentChanged -= MyMap_ExtentChanged; foreach (var layer in map.Layers) if (!string.Equals(layer.ID, "BaseLayer", StringComparison.InvariantCultureIgnoreCase)) otherLayers.Add(layer); map.Layers.Clear(); ArcGISTiledMapServiceLayer arcgisLayer = new ArcGISTiledMapServiceLayer() { ID = "BaseLayer", Url = ((RadioButton)sender).Tag as string }; this.LayoutRoot.Children.Remove(map); Map newMap = new Map() { Name = "MyMap" }; newMap.SetValue(Grid.RowProperty, 0); this.LayoutRoot.Children.Insert(0, newMap); newMap.Layers.Add(arcgisLayer); newMap.Loaded += MyMap_Loaded; newMap.ExtentChanged += MyMap_ExtentChanged; foreach (var layer in otherLayers) newMap.Layers.Add(layer);
private void MyMap_ExtentChanged(object sender, ExtentEventArgs e) { ArcGISTiledMapServiceLayer baseLayer = this.MyMap.Layers[0] as ArcGISTiledMapServiceLayer; }