]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/leaflet.osm.vector.js
Add MapTiler OMT vector map
[rails.git] / app / assets / javascripts / leaflet.osm.vector.js
1 //= require maplibre-gl
2 //= require @maplibre/maplibre-gl-leaflet
3 //= require @maptiler/maplibre-gl-omt-language
4
5 maplibregl.setRTLTextPlugin(OSM.RTL_TEXT_PLUGIN, true);
6
7 L.OSM.OpenMapTiles = L.MaplibreGL.extend({
8   initialize: function (options) {
9     L.MaplibreGL.prototype.initialize.call(this, {
10       maxZoom: 23,
11       style:
12         "https://api.maptiler.com/maps/openstreetmap/style.json?key=" +
13         options.apikey,
14       ...options
15     });
16   },
17   onAdd: function (map) {
18     L.MaplibreGL.prototype.onAdd.call(this, map);
19     const maplibreMap = this.getMaplibreMap();
20     const supportedLanguages = maplibregl.Map.prototype.supportedLanguages;
21     for (const preferredLanguage of OSM.preferred_languages) {
22       const normalizedPreferredLanguage = preferredLanguage
23         .toLowerCase()
24         .replace("-", "_");
25       const matchedLanguage = supportedLanguages.find(
26         (supported) => supported.toLowerCase() === normalizedPreferredLanguage
27       );
28       if (matchedLanguage) {
29         maplibreMap.setLanguage(matchedLanguage);
30         break;
31       }
32     }
33   },
34   onRemove: function (map) {
35     L.MaplibreGL.prototype.onRemove.call(this, map);
36   }
37 });