-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy load Maplibre GL JS and add hillshading layer
- Loading branch information
Showing
11 changed files
with
314 additions
and
15 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* Only load Maplibre bundles when layer is actually added, using dynamic imports | ||
*/ | ||
BR.MaplibreGlLazyLoader = L.Layer.extend({ | ||
initialize: function (options) { | ||
this.options = options; | ||
}, | ||
|
||
onAdd: function (map) { | ||
if (!('maplibreGL' in L)) { | ||
this._load(); | ||
} else { | ||
this._addGlLayer(); | ||
} | ||
return this; | ||
}, | ||
|
||
onRemove: function (map) { | ||
this._map.removeLayer(this.glLayer); | ||
this.glLayer = null; | ||
return this; | ||
}, | ||
|
||
// needed when overlay, also requires `position: absolute` (see css) | ||
setZIndex: function (zIndex) { | ||
this.options.zIndex = zIndex; | ||
return this; | ||
}, | ||
|
||
setOpacity: function (opacity) { | ||
if (this.glLayer) { | ||
const glMap = this.glLayer.getMaplibreMap(); | ||
if (glMap.getLayer('hillshading')) { | ||
glMap.setPaintProperty('hillshading', 'hillshade-exaggeration', opacity); | ||
} else { | ||
glMap.getCanvas().style.opacity = opacity; | ||
} | ||
} | ||
}, | ||
|
||
_load: async function () { | ||
await import('./maplibre-gl.js'); | ||
await import('./leaflet-maplibre-gl.js'); | ||
|
||
this._addGlLayer(); | ||
}, | ||
|
||
_addGlLayer: function () { | ||
this.glLayer = L.maplibreGL(this.options); | ||
this._map.addLayer(this.glLayer); | ||
|
||
this._updateZIndex(); | ||
}, | ||
|
||
_updateZIndex: function () { | ||
if (this.glLayer && this.glLayer.getContainer() && this.options.zIndex != null) { | ||
this.glLayer.getContainer().style.zIndex = this.options.zIndex; | ||
} | ||
}, | ||
}); | ||
|
||
BR.maplibreGlLazyLoader = function (options) { | ||
return new BR.MaplibreGlLazyLoader(options); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"version": 8, | ||
"sources": { | ||
"dem": { | ||
"type": "raster-dem", | ||
"tiles": [ | ||
"https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png" | ||
], | ||
"encoding": "terrarium", | ||
"tileSize": 256, | ||
"maxzoom": 15 | ||
} | ||
}, | ||
"layers": [ | ||
{ | ||
"id": "hillshading", | ||
"source": "dem", | ||
"type": "hillshade" | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"geometry": null, | ||
"properties": { | ||
"attribution": { | ||
"text": "Terrain Tiles from AWS", | ||
"url": "https://registry.opendata.aws/terrain-tiles/" | ||
}, | ||
"id": "terrarium-hillshading", | ||
"name": "Hillshading", | ||
"overlay": true, | ||
"type": "mvt", | ||
"url": "terrarium-hillshading-style" | ||
}, | ||
"type": "Feature" | ||
} |
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
Oops, something went wrong.