Select to view content in your preferred language

Catch a failure to load map when an exception isn't being thrown

1921
4
06-15-2010 08:02 AM
TJBara
by
Emerging Contributor
I have code that looks something like this:

            map = new esri.Map("mapDiv");
            dojo.connect(map, "onLoad", DoSomething);
            map.addLayer(service);

If for some reason the service isn't being added, then the OnLoad event doesn't happen and DoSomething doesn't get called.

If the map fails to load, I would like to report back to the user so the user knows there is a problem and doesn't waif for DoSomething.

Is there a way to catch this and handle it gracefully - note that the addLayer method call is not throwing an exception.

Thank you in advance.

TJ
4 Replies
AdamPfister
Esri Contributor
Take a look at the description of the 'onLoad' event from the documentation (http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/map.htm#onLoad):

Fires when the first or base layer has been successfully added to the map.


It looks like it won't fire until you add your first layer to the map.

If you want to catch errors look at the 'onLayerAddResult' event (http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/map.htm#onLayersAddResult):

onLayerAddResult(layer, error)  Fires after specified layer has been added to the map.


in that event you can look for an error.

or maybe you can wrap the map initialization in a try/catch block?
try {
     map = new esri.Map()...
} catch (e) {
    //get error message
}
0 Kudos
TJBara
by
Emerging Contributor
The "stipulation" of successfully loading the base layer onto a map you reference before OnLoad fires is actually the issue I was describing.

It wasn't throwing an error I could catch in a try/catch block.

I will look into the OnLayerAddResult event.

Thank you.
0 Kudos
TJBara
by
Emerging Contributor
The "stipulation" of successfully loading the base layer onto a map you reference before OnLoad fires is actually the issue I was describing.

It wasn't throwing an error I could catch in a try/catch block.

I will look into the OnLayerAddResult event.

Thank you.


It looks like the OnLayerAddResult is part of the Beta 2.0 Javascript API.  We are using 1.6, so it is not possible to assess this would be useful int the future.

So the question for me remains is there way to catch the fact that the map did not successfully add a layer.

TJ
0 Kudos
AdamPfister
Esri Contributor
maybe tie into the onLoad and/or onError events of the Layer rather than the events of the map?
0 Kudos