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

Add OpenMapTiles vector map #4042

Open
wants to merge 6 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
52 changes: 52 additions & 0 deletions app/assets/javascripts/leaflet.map.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//= require maplibre-gl
//= require @maplibre/maplibre-gl-leaflet
//= require @maptiler/maplibre-gl-omt-language
//= require i18n
//= require qs/dist/qs

L.extend(L.LatLngBounds.prototype, {
Expand All @@ -11,6 +15,40 @@ L.extend(L.LatLngBounds.prototype, {
}
});

if (OSM.MAPTILER_KEY) {
L.OpenMapTiles = L.MaplibreGL.extend({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arabic and Hebrew would be broken unless this code also installs the right-to-left text plugin. (Note that it only supports these two languages, not other languages in the same scripts or other right-to-left scripts.)

options: {
maxZoom: 23,
style: "https://api.maptiler.com/maps/openstreetmap/style.json?key=" + OSM.MAPTILER_KEY
},
onAdd: function (map) {
var supportedLanguages = "am,ar,az,be,bg,br,bs,ca,co,cs,cy,da,de,el,en,eo,es,et,eu,fi,fr,fy,ga,gd,he,hi,hr,hu,hy,id,is,it,ja,ja_kana,ja_rm,ja-Latn,ja-Hira,ka,kk,kn,ko,ko-Latn,ku,la,lb,lt,lv,mk,mt,ml,nl,no,oc,pl,pt,rm,ro,ru,sk,sl,sq,sr,sr-Latn,sv,ta,te,th,tr,uk,zh".split(",");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name:ja_kana and name:ja_rm are both documented on the wiki as being deprecated, although they are still in use to some extent. Is it a good idea to support these nonstandard language codes as part of the website?

Copy link
Contributor

@1ec5 1ec5 Jun 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list doesn’t include the languages added to OpenMapTiles openmaptiles/openmaptiles#1477, including zh-Hans and zh-Hant. I assume that’s because the MapTiler-hosted tiles are on OpenMapTiles v3.14, whereas this change is still waiting to be released. Is there a way to automate this list so that the website maintainers won’t have to remember to update it based on OpenMapTiles’ release schedule and MapTiler’s upgrade schedule?

L.MaplibreGL.prototype.onAdd.call(this, map);
var m = this.getMaplibreMap();
m.on("load", function () {
var locale;
if (supportedLanguages.includes(I18n.locale)) {
locale = I18n.locale;
} else {
var mainLocale = I18n.locale.slice(0, 2);
var localeIndex = supportedLanguages.findIndex(function (locale) { return locale.slice(0, 2) === mainLocale; });
if (localeIndex > -1) {
locale = supportedLanguages[localeIndex];
}
}

if (locale) {
m.setLanguage(locale);
}
});
L.MaplibreGL.prototype._update.call(this, map);
},
onRemove: function (map) {
L.MaplibreGL.prototype.onRemove.call(this, map);
}
});
}

L.OSM.Map = L.Map.extend({
initialize: function (id, options) {
L.Map.prototype.initialize.call(this, id, options);
Expand Down Expand Up @@ -65,6 +103,11 @@ L.OSM.Map = L.Map.extend({
}).prop("outerHTML");
var hotosm = I18n.t("javascripts.map.hotosm_credit", { hotosm_link: hotosm_link, osm_france_link: osm_france_link });

var openmaptiles_link = I18n.t("javascripts.map.openmaptiles", {
openmaptiles_url: "https://openmaptiles.org/",
maptiler_url: "https://www.maptiler.com/"
});

this.baseLayers = [];

this.baseLayers.push(new L.OSM.Mapnik({
Expand All @@ -74,6 +117,15 @@ L.OSM.Map = L.Map.extend({
name: I18n.t("javascripts.map.base.standard")
}));

if (L.OpenMapTiles) {
this.baseLayers.push(new L.OpenMapTiles({
attribution: copyright + ". " + openmaptiles_link + ". " + terms,
code: "V",
keyid: "openmaptiles_osm",
name: I18n.t("javascripts.map.base.openmaptiles_osm")
}));
}

this.baseLayers.push(new L.OSM.CyclOSM({
attribution: copyright + ". " + cyclosm + ". " + terms,
code: "Y",
Expand Down
4 changes: 4 additions & 0 deletions app/assets/javascripts/osm.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ OSM = {
THUNDERFOREST_KEY: <%= Settings.thunderforest_key.to_json %>,
<% end %>

<% if Settings.key?(:maptiler_key) %>
MAPTILER_KEY: <%= Settings.maptiler_key.to_json %>,
<% end %>

MARKER_GREEN: <%= image_path("marker-green.png").to_json %>,
MARKER_RED: <%= image_path("marker-red.png").to_json %>,

Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2953,6 +2953,7 @@ en:
transport_map: Transport Map
hot: Humanitarian
opnvkarte: ÖPNVKarte
openmaptiles_osm: OpenMapTiles
layers:
header: Map Layers
notes: Map Notes
Expand All @@ -2973,6 +2974,7 @@ en:
memomaps: MeMoMaps
hotosm_credit: "Tiles style by %{hotosm_link} hosted by %{osm_france_link}"
hotosm_name: Humanitarian OpenStreetMap Team
openmaptiles: "Vector tiles from <a href='%{openmaptiles_url}' target='_blank'>OpenMapTiles</a> hosted by <a href='%{maptiler_url}' target='_blank'>MapTiler</a>"
site:
edit_tooltip: Edit the map
edit_disabled_tooltip: Zoom in to edit the map
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
"name": "openstreetmap",
"private": true,
"dependencies": {
"@maplibre/maplibre-gl-leaflet": "^0.0.17",
"@maptiler/maplibre-gl-omt-language": "^0.0.1",
"bs-custom-file-input": "^1.3.4",
"jquery-simulate": "^1.0.2",
"js-cookie": "^3.0.0",
"leaflet": "^1.8.0",
"maplibre-gl": "^2.4.0",
"leaflet.locatecontrol": "^0.79.0",
"osm-community-index": "^5.2.0",
"qs": "^6.9.4"
Expand Down
Loading