openlayers icon indicating copy to clipboard operation
openlayers copied to clipboard

Two maps, same layers

Open Charl20cas opened this issue 6 years ago • 3 comments

I have two different maps with different layers and different views.

var roadLayer = new TileLayer({
  source: new TileJSON({
    url: "https://a.tiles.mapbox.com/v3/aj.1x1-degrees.json",
    crossOrigin: ""
  })
});

var otherLayer = new TileLayer({
  source: new TileJSON({
    url: "https://a.tiles.mapbox.com/v3/aj.1x1-degrees.json",
    crossOrigin: ""
  })
});

var view = new View({
  center: [-6655.5402445057125, 6709968.258934638],
  zoom: 6
});

var map1 = new Map({
  target: "roadMap",
  layers: [roadLayer],
  view: view
});

var map2 = new Map({
  layers: [otherLayer],
  target: "aerialMap",
  view: new View({
    center: [-6655.5402445057125, 6709968.258934638],
    zoom: 1
  })
});

I need both maps to use the same layers but when I put the same layers only one of the two maps is painted.

var map1 = new Map({
  target: "roadMap",
  layers: [roadLayer],
  view: view
});

var map2 = new Map({
  layers: [roadLayer],
  target: "aerialMap",
  view: new View({
    center: [-6655.5402445057125, 6709968.258934638],
    zoom: 1
  })
});

In version 5.3.3 with WEBGLMap it worked fine, but in version 6.3.1 with Map it didn't.

Is this possible to do?

https://codesandbox.io/s/hopeful-wood-3bfxb?file=/index.js:660-670

Charl20cas avatar Jun 05 '20 08:06 Charl20cas

Not any more, each layer is now a separate HTML element, so it can't be rendered in two maps simultaneously.

See: https://github.com/openlayers/openlayers/blob/master/changelog/v6.0.0.md#layers-can-only-be-added-to-a-single-map

jahow avatar Jun 05 '20 08:06 jahow

But just as with an overview map https://openlayers.org/en/latest/examples/overviewmap.html you can use the same source in two layers

var roadSource = new TileJSON({
  url: "https://a.tiles.mapbox.com/v3/aj.1x1-degrees.json",
  crossOrigin: ""
});

var map1 = new Map({
  target: "roadMap",
  layers: [new TileLayer({source: roadSource})],
  view: view
});

var map2 = new Map({
  layers: [new TileLayer({source: roadSource})],
  target: "aerialMap",
  view: new View({
    center: [-6655.5402445057125, 6709968.258934638],
    zoom: 1
  })
});

mike-000 avatar Jun 05 '20 09:06 mike-000

That's what I'm testing ... the problem is the TileLayer. If I manually add the layers of the first map (minus the TileLayer) to the second map it also works for me.

Charl20cas avatar Jun 05 '20 09:06 Charl20cas