-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bundle maplibre-layers statically as a mapbox wrapper
- Loading branch information
Showing
9 changed files
with
57,134 additions
and
32,116 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,117 @@ | ||
import Layer from 'ol/layer/Layer' | ||
import { toDegrees } from 'ol/math' | ||
import { toLonLat } from 'ol/proj' | ||
import { toLonLat } from 'ol/proj.js' | ||
|
||
import maplibregl from 'maplibre-gl' | ||
import mapboxgl from 'mapbox-gl' | ||
|
||
/** | ||
* @typedef {Object} Options | ||
* @property {Object<string, *>} maplibreOptions | ||
* @property {string} [accessToken] | ||
* @property {string} style | ||
* @property {string|HTMLElement} container | ||
*/ | ||
|
||
export default class MapLibreLayer extends Layer { | ||
export default class MapBox extends Layer { | ||
/** | ||
* @param {Options} options | ||
*/ | ||
constructor(options) { | ||
const baseOptions = Object.assign({}, options) | ||
|
||
delete baseOptions.maplibreOptions | ||
// baseOptions.xyz = options.maplibreOptions.xyz | ||
// baseOptions.xyz_custom = options.maplibreOptions.xyz_custom | ||
delete baseOptions.accessToken | ||
delete baseOptions.style | ||
delete baseOptions.container | ||
delete baseOptions.xyz | ||
|
||
super(baseOptions) | ||
|
||
// write directly to properties | ||
this.set('xyz', options.maplibreOptions.xyz) | ||
this.set('xyz_custom', options.maplibreOptions.xyz_custom) | ||
|
||
// const container = options.maplibreOptions.container | ||
this.map_ = options.maplibreOptions.map | ||
|
||
const container = document.createElement('div') | ||
container.style.width = '100%' | ||
container.style.height = '100%' | ||
container.style.position = 'absolute' | ||
container.style.zIndex = -50 | ||
this.map_.getTarget().parentElement.appendChild(container) | ||
|
||
this.maplibreMap = new maplibregl.Map( | ||
Object.assign({}, options.maplibreOptions, { | ||
container: container, | ||
attributionControl: false, | ||
interactive: false, | ||
trackResize: false, | ||
}) | ||
) | ||
this.xyz_ = options.xyz | ||
|
||
if (options.accessToken) { | ||
mapboxgl.accessToken = options.accessToken | ||
} | ||
|
||
this.applyOpacity_() | ||
this.map_ = new mapboxgl.Map({ | ||
container: options.maplibreOptions.container, | ||
style: options.maplibreOptions.style, | ||
attributionControl: false, | ||
interactive: false, | ||
}) | ||
this.maplibreMap = this.map_ | ||
} | ||
|
||
/** | ||
* @param {number} opacity | ||
*/ | ||
setOpacity(opacity) { | ||
super.setOpacity(opacity) | ||
this.applyOpacity_() | ||
getMapBoxMap() { | ||
return this.map_ | ||
} | ||
|
||
applyOpacity_() { | ||
const canvas = this.maplibreMap.getCanvas() | ||
const opacity = this.getOpacity().toString() | ||
if (opacity !== canvas.style.opacity) { | ||
canvas.style.opacity = opacity | ||
} | ||
getMapLibreMap() { | ||
return this.map_ | ||
} | ||
|
||
/** | ||
* @param {import('ol/Map').FrameState} frameState | ||
* @return {HTMLCanvasElement} canvas | ||
* @param {import('ol/PluggableMap').FrameState} frameState | ||
*/ | ||
render(frameState) { | ||
const canvas = this.maplibreMap.getCanvas() | ||
const canvas = this.map_.getCanvas() | ||
const viewState = frameState.viewState | ||
|
||
// adjust view parameters in maplibre | ||
this.maplibreMap.jumpTo({ | ||
canvas.style.position = 'absolute' | ||
|
||
const visible = this.getVisible() | ||
canvas.style.display = visible ? 'block' : 'none' | ||
|
||
const opacity = this.getOpacity().toString() | ||
if (opacity !== canvas.style.opacity) { | ||
canvas.style.opacity = opacity | ||
} | ||
|
||
// adjust view parameters in mapbox | ||
const rotation = viewState.rotation | ||
if (rotation) { | ||
this.map_.rotateTo(toDegrees(-rotation), { | ||
animate: false, | ||
}) | ||
} | ||
this.map_.jumpTo({ | ||
center: toLonLat(viewState.center), | ||
zoom: viewState.zoom - 1, | ||
bearing: toDegrees(-viewState.rotation), | ||
animate: false, | ||
}) | ||
|
||
const maplibreCanvas = this.maplibreMap.getCanvas() | ||
if (!maplibreCanvas.isConnected) { | ||
// The canvas is not connected to the DOM, request a map rendering at the next animation frame | ||
// to set the canvas size. | ||
this.map_.render() | ||
// this.getMapInternal().render(); | ||
} else if (!sameSize(maplibreCanvas, frameState)) { | ||
this.maplibreMap._container.style.width = `${frameState.size[0]}px` | ||
this.maplibreMap._container.style.height = `${frameState.size[1]}px` | ||
this.maplibreMap._container.style.position = 'absolute' | ||
this.maplibreMap.resize() | ||
this.maplibreMap._container.style.position = 'absolute' | ||
// this.maplibreMap._container.style.display = 'none' | ||
// cancel the scheduled update & trigger synchronous redraw | ||
// see https://github.com/mapbox/mapbox-gl-js/issues/7893#issue-408992184 | ||
// NOTE: THIS MIGHT BREAK WHEN UPDATING MAPBOX | ||
if (this.map_._frame) { | ||
this.map_._frame.cancel() | ||
this.map_._frame = null | ||
} | ||
this.map_._render() | ||
|
||
// this.maplibreMap.redraw(); | ||
// if (this.maplibreMap._frame) { | ||
// this.maplibreMap._frame.cancel() | ||
// this.maplibreMap._frame = null | ||
// } | ||
// this.maplibreMap._render() | ||
|
||
// return this.maplibreMap.getContainer(); | ||
return canvas | ||
} | ||
|
||
/** | ||
* @param {string} name | ||
* @param {boolean} visible | ||
*/ | ||
setLayerVisibility(name, visible) { | ||
this.map_.setLayoutProperty( | ||
name, | ||
'visibility', | ||
visible ? 'visible' : 'none' | ||
) | ||
} | ||
|
||
/** | ||
* @return {mapboxgl.Style} | ||
*/ | ||
getStyle() { | ||
return this.map_.getStyle() | ||
} | ||
|
||
getXYZ() { | ||
return this.get('xyz') | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {HTMLCanvasElement} canvas | ||
* @param {import('ol/Map').FrameState} frameState | ||
* @return boolean | ||
*/ | ||
function sameSize(canvas, frameState) { | ||
return ( | ||
canvas.width === Math.floor(frameState.size[0] * frameState.pixelRatio) || | ||
canvas.height === Math.floor(frameState.size[1] * frameState.pixelRatio) | ||
) | ||
} |