Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a TMS layer that I'm using as my map, i have set it up so that users can change the map.
However, changing the layer does not update the map view until I hit the zoom in button or the zoom out button. How can I refresh the map view to show the newly added layer?
Here is a quick code sample. I tried layer.redraw() without any solution. Does someone have any other ideas.

    function initMAP() {
    var mapBounds = new OpenLayers.Bounds(bounds[0], bounds[1], bounds[2], bounds[3]);
    var mapMinZoom = 0;
    var mapMaxZoom = 3;

    // avoid pink tiles
    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
    OpenLayers.Util.onImageLoadErrorColor = "transparent";




    var options = {
        theme: null,
        controls: [

        new OpenLayers.Control.Navigation({
            dragPanOptions: {
                //     enableKinetic: true
            }
        }),
        new OpenLayers.Control.Attribution(),
        new OpenLayers.Control.Zoom()
        ],

        maxExtent: new OpenLayers.Bounds(bounds[0], bounds[1], bounds[2], bounds[3]),
        maxResolution: 4,
        numZoomLevels: 3

    };


    map = new OpenLayers.Map('map', options);

      layer = new OpenLayers.Layer.TMS("TMS", "http://mymapserver.com/MAP2/", { 'type': 'png', 'getURL': get_my_url });


      map.addLayer(layer);
      layer.redraw({ force: true });

      map.zoomToExtent(mapBounds);

    new OpenLayers.Control.Attribution(),
    new OpenLayers.Control.Zoom()

}

Code where I'm caching the layer:

function get_my_url(bounds) {


    var res = this.map.getResolution();
    var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
    var y = Math.round((bounds.bottom - this.maxExtent.bottom) / (res * this.tileSize.h));
    var z = this.map.getZoom();

    var path = z + "/" + x + "/" + y + "." + this.type;
    if (WorkingMap == "4")
    {
        var url = "../TrentonArtsFestival/"

    }
    if (WorkingMap == "2")
    {
        var url=""

    }


    if (url instanceof Array) {
        url = this.selectUrl(path, url);
    }
    return url + path;

}
share|improve this question
    
on a desperate attempt i just try setting the map div html to "" and it finally updated the layer. is this the proper way to do this? –  user677275 Aug 31 '13 at 8:14
    
Can you post or highlight the code where you are changing the Layer? That part of the cold holds the key to your problem. –  Devdatta Tengshe Aug 31 '13 at 9:14
    
ok i updated it. –  user677275 Aug 31 '13 at 9:33
    
Do not change the url of an existing layer. Initialise a new layer with the new url, and add it ti the map, and remove the old layer. That should work out. –  Devdatta Tengshe Aug 31 '13 at 11:29
    
a code sample would go a long way. I though I was already doing what you suggested in the above code. –  user677275 Sep 4 '13 at 0:47
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.