Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo bind calls on source #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</head>
<body>
<div class="map" id="map"></div>
<p>Demo bind source calls: <button onclick="refreshSource()">Refresh source</button></p>
<script src="./demo.js"></script>
</body>
</html>
30 changes: 18 additions & 12 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@ import 'ol/ol.css';
import './style.css';

import Map from 'ol/Map';
import Source from 'ol/source/Source';
import Source from '../src/MapLibreSource';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import {TileDebug} from 'ol/source';

import MapLibreLayer from '../src/MapLibreLayer';

const mapLibreLayer = new MapLibreLayer({
maplibreOptions: {
style:
'https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte_world.vt/style.json',
},
source: new Source({
attributions: [
'<a href="https://www.geo.admin.ch/en/geo-services/geo-services/portrayal-services-web-mapping/vector_tiles_service.html" target="_blank">© swisstopo</a>',
],
}),
});

window.map = new Map({
layers: [
new MapLibreLayer({
maplibreOptions: {
style:
'https://vectortiles.geo.admin.ch/styles/ch.swisstopo.leichte-basiskarte_world.vt/style.json',
},
source: new Source({
attributions: [
'<a href="https://www.geo.admin.ch/en/geo-services/geo-services/portrayal-services-web-mapping/vector_tiles_service.html" target="_blank">© swisstopo</a>',
],
}),
}),
mapLibreLayer,
new TileLayer({
source: new TileDebug(),
}),
Expand All @@ -32,3 +34,7 @@ window.map = new Map({
zoom: 8,
}),
});

window.refreshSource = () => {
mapLibreLayer.getSource().refresh();
};
2 changes: 1 addition & 1 deletion examples/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ body,
margin: 0;
padding: 0;
width: 100%;
height: 100%;
height: 90%;
}
4 changes: 4 additions & 0 deletions src/MapLibreLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export default class MapLibreLayer extends Layer {
})
);

const source = this.getSource();
if (source.setMapLibreMap) {
source.setMapLibreMap(this.maplibreMap);
}
this.applyOpacity_();
}

Expand Down
29 changes: 29 additions & 0 deletions src/MapLibreSource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Source from 'ol/source/Source';

export default class MapLibreSource extends Source {
constructor(options) {
super(options);
}

/**
* Set the reference to MapLibre Map instance.
*/
setMapLibreMap(mapLibreMap) {
this.mapLibreMap = mapLibreMap;
}

/**
* Refresh the Ol Source, but also EVERY MapLibre source.
*/
refresh() {
if (!this.mapLibreMap) {
return;
}
const sourcesObjects = this.mapLibreMap.style.sourceCaches;
if (!sourcesObjects) {
return;
}
Object.values(sourcesObjects).forEach((source) => source.clearTiles());
super.refresh();
}
}