Select to view content in your preferred language

Changing ArcGISTiledMapServiceLayer (in a different spatial reference) at runtime

2330
2
09-21-2010 12:05 PM
SanjayRajput
Deactivated User
I am loading ArcGISTiledMapServiceLayer during startup of silverlight app (let's say in spatial reference 102100). At runtime, say click of a button I need to change the Map ArcGISTiledMapServiceLayer to point to different layer in a spatial reference say 2229. How it can be achived in the ESRI Silverlight app? I am NOT adding 2 ArcGISTiledMapServiceLayer layers in a different spatial reference instead I am just replacing it. I will appreciate if someone could please post a code snippet to do it. Here is the snapshot of .xaml and .xaml.cs:

MainPage.xaml

<Grid x:Name="LayoutRoot" Background="White">
        <esri:Map x:Name="MyMap" ExtentChanged="MyMap_ExtentChanged">
            <esri:ArcGISTiledMapServiceLayer Url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer" />
        </esri:Map>
        <Button Content="Replace Layer" Click="Button_Click" VerticalAlignment="Top" HorizontalAlignment="Center"/>
    </Grid>

MainPage.xaml.cs

private void Button_Click(object sender, RoutedEventArgs e)
        {
            ArcGISTiledMapServiceLayer newtiledlayer = new ArcGISTiledMapServiceLayer() { Url = "http://usl-am-dev-08/ArcGIS/rest/services/CalleguasMWD_BaseMap_V0.3/MapServer" };
            newtiledlayer.Initialize();
            ArcGISTiledMapServiceLayer tiledLayer = this.MyMap.Layers[0] as ArcGISTiledMapServiceLayer;
            if (tiledLayer != null)
            {
                this.MyMap.Layers.Remove(tiledLayer);

                this.MyMap.Layers.Add(newtiledlayer);
              
            }
        }
0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor
A couple of rules:

1. A tiled layer with a different spatial reference than that of the map will never display.
2. The first layer in the map defines the SR (unless SR was explicitly defined at startup) for the lifetime of the map (exception: See #3)
3. Once an SR for a map has been set, it cannot be changed unless you clear the entire Layer Collection, and either set the Extent to null or an extent with the new desired spatial reference, then add the layers you want (if you set the extent to null, see #2, else see #1).

So to make your code work after calling 'this.MyMap.Layers.Remove(tiledLayer)', set the map's extent to null (note however that this will only work if the tiled layer is the only layer in the map. If not, you will have to remove all layers first)
0 Kudos
SanjayRajput
Deactivated User
Morten,
    Thanks for your reply. Its working now. I appreciate it. Thanks again.

Sanjay.
 

A couple of rules:

1. A tiled layer with a different spatial reference than that of the map will never display.
2. The first layer in the map defines the SR (unless SR was explicitly defined at startup) for the lifetime of the map (exception: See #3)
3. Once an SR for a map has been set, it cannot be changed unless you clear the entire Layer Collection, and either set the Extent to null or an extent with the new desired spatial reference, then add the layers you want (if you set the extent to null, see #2, else see #1).

So to make your code work after calling 'this.MyMap.Layers.Remove(tiledLayer)', set the map's extent to null (note however that this will only work if the tiled layer is the only layer in the map. If not, you will have to remove all layers first)
0 Kudos