Select to view content in your preferred language

Can a layer URL be swapped?

768
4
Jump to solution
05-08-2013 12:13 PM
TyroneLigon
Deactivated User
In my app I have 3 layers; each layer has an array with 2-3 endpoint URLs associated with it. When the app first loads and I create the layers, I pull the first URL from the layer's associated array, apply display levels, and add the layer to the map.

I have an "onError" function that determines which layer failed, pulls the next URL from the layer's URL array, and assigns that URL to the layer. Problem: it seems that the URL can't be switched. Do I have to remove the layer from the map, re-create the layer with the next URL, and add the layer back to the map?
0 Kudos
1 Solution

Accepted Solutions
StephenLead
Honored Contributor
Do I have to remove the layer from the map, re-create the layer with the next URL, and add the layer back to the map?


I believe that this is the case.

View solution in original post

0 Kudos
4 Replies
StephenLead
Honored Contributor
Do I have to remove the layer from the map, re-create the layer with the next URL, and add the layer back to the map?


I believe that this is the case.
0 Kudos
TyroneLigon
Deactivated User
Final resolution: remove the failing map layer, re-create the layer after getting the next endpoint URL, and add it to the map.

Note: if you add a dojo.connect statement to the layer, the statement ceases to exist when you delete the layer; when the layer is re-created and added to the map, you also have to re-create the connect statement. Discovered this little gem when testing what happens when the second endpoint URL also generates an error; lost about 1 1/2 hours trying to figure out why my URL switch code didn't work when more than one URL was invalid.
0 Kudos
StephenLead
Honored Contributor
Note: if you add a dojo.connect statement to the layer, the statement ceases to exist when you delete the layer; when the layer is re-created and added to the map, you also have to re-create the connect statement.


You could attach the connect statement to a variable, so that you could disconnect it explicitly when the layer failed:

var listener = dojo.connect(layerName, "onError", removeLayer);
function removeLayer() {
    //disconnect the listener
    dojo.disconnect(listener);

    //code to remove the layer
}


my URL switch code didn't work when more than one URL was invalid.


A more pressing question might be why so many of your URLs are failing. Is that something you can solve? It's pretty unusual to need to resort to this layer replacement.

Good luck,
Steve
0 Kudos
TyroneLigon
Deactivated User
Steve,

I'm writing code to handle situations where the map service is not available; to simulate that scenario, I deliberately misspell at least one of the map service names in the layer's URL array. I've gotten to the point where the error code works fine except for one scenario: the first layer added to the map fails. There's another ticket that somewhat addresses this scenario, so I will add to that ticket's thread.
0 Kudos