taigi100
taigi100

Reputation: 2839

Changing Source url of a XYZ layer and redrawing the layer/map?

I wanna change the url of my ol3 map source. I've tryed using things such as map.set or map.getlayers().set or whatever but I can't seem to find a way to access my source object. Here's the code:

function init() {
    var map = new ol.Map({
        target: 'map',
        layers: [
            new ol.layer.Tile({
                source: new ol.source.XYZ({
                    projection: 'PIXELS',
                    tileGrid: mapTileGrid,
                    url: loc
                })
            })
        ],
        view: new ol.View({
            projection: ol.proj.get('PIXELS'),
            extent: mapExtent,
             maxResolution: mapTileGrid.getResolution(0)
             })
        });

map.getView().fit(mapExtent, map.getSize());
    console.log(map.get("layergroup").get("layers").get("url"));
    map.get("layergroup").get("layers").set("url",loc);
}

What's a way to change the url property and reload the layer ?

I also tried using the setSource function as in the following answer : here but it seems to not work (can't setSource of undefined).

Upvotes: 2

Views: 1555

Answers (1)

pavlos
pavlos

Reputation: 3081

try the following

function init() {
    var map = new ol.Map({
        target: 'map',
        layers: [
            new ol.layer.Tile({
                source: new ol.source.XYZ({
                    projection: 'PIXELS',
                    tileGrid: mapTileGrid,
                    url: loc
                })
            })
        ],
        view: new ol.View({
            projection: ol.proj.get('PIXELS'),
            extent: mapExtent,
             maxResolution: mapTileGrid.getResolution(0)
             })
        });

map.getView().fit(mapExtent, map.getSize());
//get alll the layers exist on your map
var layers = map.getLayers();
//lets assume you want to set the url for the first layer found
layers[0].getSource().setUrl(loc); 
}

Upvotes: 1

Related Questions