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

Attach to Mapbox GL JS if present #13

Merged
merged 1 commit into from
Feb 1, 2025
Merged
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# MapLibre GL Dates

This is a plugin for [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js/) for filtering the map based on a date. The plugin is designed for use with [OpenHistoricalMap](https://www.openhistoricalmap.org/) data.
This is a plugin for [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js/) for filtering the map based on a date. The plugin is designed for use with [OpenHistoricalMap](https://www.openhistoricalmap.org/) data.

## Requirements

This plugin requires [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js/) v3.0.0 and above.
This plugin requires [MapLibre GL JS](https://github.com/maplibre/maplibre-gl-js/) v3.0.0 and above. It also works in [Mapbox GL JS](https://docs.mapbox.com/mapbox-gl-js/) when installed as a standalone script (without using a package manager).

This plugin is able to manipulate both the deprecated [legacy filter syntax](https://maplibre.org/maplibre-style-spec/deprecations/#other-filter) and the newer [expression syntax](https://maplibre.org/maplibre-style-spec/expressions/) defined in the MapLibre Style Specification.

Expand All @@ -31,6 +31,8 @@ This plugin is available as [an NPM plugin](https://www.npmjs.com/package/@openh
npm install @openhistoricalmap/maplibre-gl-dates
```

Alternatively, you can include the plugin as a standalone script from a CDN such as [unpkg](https://unpkg.com/@openhistoricalmap/maplibre-gl-dates/index.js). Use this option to filter a style in Mapbox GL JS.

## Usage

After creating an instance of `maplibregl.Map`, register an event listener for the `styledata` event that filters the map:
Expand Down
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,17 @@ function updateVariable(letExpression, name, newValue) {
}
}

if (typeof window !== 'undefined' && 'maplibregl' in window) {
maplibregl.Map.prototype.filterByDate = function (date) {
filterByDate(this, date);
};
if (typeof window !== 'undefined' && ('maplibregl' in window || 'mapboxgl' in window)) {
if ('maplibregl' in window) {
maplibregl.Map.prototype.filterByDate = function (date) {
filterByDate(this, date);
};
}
if ('mapboxgl' in window) {
mapboxgl.Map.prototype.filterByDate = function (date) {
filterByDate(this, date);
};
}
} else if (typeof module !== 'undefined') {
module.exports = {
filterByDate,
Expand Down