Changing Source url of a XYZ layer and redrawing the layer/map?
我想更改我的 ol3 地图源的 url。我尝试过使用诸如 map.set 或 map.getlayers().set 之类的东西,但我似乎找不到访问源对象的方法。这是代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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); } |
有什么方法可以改变 url 属性并重新加载图层?
我还尝试使用 setSource 函数,如以下答案所示:here
但它似乎不起作用(无法设置未定义的来源)。
试试下面的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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); } |