2

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).

2
  • Yeah, managed to fix it after some time using some simple setsource but not that simple, will post it here when I get to my pc. Commented Nov 13, 2015 at 21:09
  • @JonatasWalker var viewaux = new ol.View({ projection: ol.proj.get('PIXELS'), extent: mapExtent, maxResolution: mapTileGrid.getResolution(0), rotation: Math.PI / 5 }); layer.setSource(saux); map.setView(viewaux); As simple as that :) Commented Nov 17, 2015 at 9:27

1 Answer 1

1

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); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.