Resolution problem

1307
12
09-01-2010 07:22 AM
AgatinoLa_Rosa
Emerging Contributor
I have a map control in a silverlight 3 app with API 1.2 that shows dynamically users' map services. The first layer is an arcgisonline street layer, and on top of that a user's service. The first map displays ok and it shows a resolution of NaN(??), when reloading the map control with other services (dynamic and cached as well) I get "error on Page" and a resolution value of thousands.

How can I make this at work? Thanks.
0 Kudos
12 Replies
dotMorten_esri
Esri Notable Contributor
I'm not sure I'm completely following this one. When mention only one map, but then go on to say "the first map". Did you mean the first layer? (although a layer doesn't have a 'resolution')
If a map shows right, the resolution cannot be NaN. A layer doesn't have a resolution
When do you check the Map.Resolution property? Before or after the layers has initialized?

What error did you get on the page? Could you share the exception and the stacktrace?
0 Kudos
AgatinoLa_Rosa
Emerging Contributor
Thanks Morten, I will try to make myself clear. It is a single map with two Services, the first is an arcgisonline street service [always the same at design time], and the second is configured at run time (could be cached or dynamic according to the selected service by the user). The map control includes a ESRI navigation tool. The first time the map is displayed it goes ok, then I select another service replacing url at run time. This time it stopped at the following procedure:

private void Map_ExtentChanged(object sender, ExtentEventArgs args)
        {
   if (!double.IsNaN(Map.Resolution) && ZoomSlider != null)
    ZoomSlider.Value = ResolutionToValue(Map.Resolution);
        }

Then the map disappears and on the browser there is the following error:
Unhandled Error in Silverlight Application
Code:4004
Category:ManagedRuntimeError
Message: System.NullReferenceException.Object reference not set to an instance of an object
at ESRI.ArcGIS.Client.Map<>c_DisplayClass1b<>c_DisplayClass1e<beginZoomToExtent>b_17()

I have seen that the resolution after initialize is not NaN as you suspected, I did put the alert at the wrong location. However I think it is the change of resolution due to the change of runtime service that gives this problem. If I select always the same service at runtime and the resolution stays as same the map shows ok.

Thank you for your time.
0 Kudos
dotMorten_esri
Esri Notable Contributor
I would strongly advise against changing the Url at runtime. Depending on what service you change it to, weird things can happen.
Instead remove the layer, create a new instance and add that to the map.
0 Kudos
AgatinoLa_Rosa
Emerging Contributor
This has been one of my tries, I have created new instances as follow

if cached...
                ArcGISTiledMapServiceLayer thisCachedService = new ArcGISTiledMapServiceLayer()  
                {
                Url = ...,
                ID = "ServiceLayer"
                };                             
                this.baseMap.Layers.Insert(1, thisCachedService);
                thisCachedService.Initialize();
if dynamic...
        ArcGISDynamicMapServiceLayer thisDynamicService = new ArcGISDynamicMapServiceLayer()
                {
                 Url = ...,
                 ID = "ServiceLayer"
                };               
                this.baseMap.Layers.Insert(1, thisDynamicService);
                thisDynamicService.Initialize();


and then removed from the list of layers....

            for (int i = 0; i < this.baseMap.Layers.Count; i++)
            {
                if (this.baseMap.Layers.ID == "ServiceLayer")
                {
                    this.baseMap.Layers.RemoveAt(i);
                    break;
                }
            }

However, if I use Map.Layers.Clear() it does not read the map layers as configured at design time anymore and I need this insert/removeat procedure.

I have also disabled the navigation tool with no success. I don't know what else to try...
0 Kudos
AgatinoLa_Rosa
Emerging Contributor
After several more tries I have seen that if I remove the zoomto and panto the map shows ok for any dynamic/cached url at runtime. What I do not understand is that the very first time I load the xaml the zoomto and panto procedures work. After this time it goes blank.

What could be the problem when using zoomto/panto at runtime more than once in a silverlight map control?
0 Kudos
JenniferNery
Esri Regular Contributor
Hi,

I am not able to reproduce the problem. If I understood correctly, you need to replace the layer at run-time.

I am using the following code:
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>


Code-behind:
private void Button_Click(object sender, RoutedEventArgs e)
  {
   ArcGISDynamicMapServiceLayer dynamic = new ArcGISDynamicMapServiceLayer() { Url = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/MapServer" };   
   dynamic.Initialize();
   ArcGISTiledMapServiceLayer tiledLayer = this.MyMap.Layers[0] as ArcGISTiledMapServiceLayer;
   if (tiledLayer != null)
   {
    this.MyMap.Layers.Remove(tiledLayer);
    this.MyMap.Layers.Add(dynamic);
   }
  }
private void MyMap_ExtentChanged(object sender, ESRI.ArcGIS.Client.ExtentEventArgs e)
  {
// placed a breakpoint here to make sure the map's extent still get updated after replacing layer.
  }
0 Kudos
AgatinoLa_Rosa
Emerging Contributor
Thanks Jennifer, I have been able to replace layers at run time. However, I need to zoom/pan to the extent of the new layer in my map. My c# code has a procedure that sends a rest request to the gisserver and reads the envelope extent of the new layer, and then zoomto and panto the new envelope. The first time I set at run time the layer I see my map, after the first replacement the procedures zoomto and panto return an error on page.

So my question is why these two procedures (zoomto and panto) return an error.
0 Kudos
JenniferNery
Esri Regular Contributor
It's hard to tell without knowing the error message or stack trace.  Could you share your code?

Thanks.
0 Kudos
AgatinoLa_Rosa
Emerging Contributor
it is on this thread reply # 3. Thanks.
0 Kudos