Skip to content

Commit e22e1a9

Browse files
committed
Implements dynamic basemap from tile sources
1 parent e602f34 commit e22e1a9

File tree

2 files changed

+28
-37
lines changed

2 files changed

+28
-37
lines changed

app/views/projects/form/_map.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
<%= content_tag(:div, "", :data => {
66
:geom => @project.geojson,
7+
:layers => @project.gtt_tile_sources,
78
:edit => 'Polygon'
89
}, :id => 'ol-' + rand(36**8).to_s(36), :class => 'ol-map')
910
%>

assets/javascripts/app.map.js

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ App.map = (function ($, publ) {
88

99
var map, vector, bounds, contents, toolbar, geolocation = null;
1010
var features = [];
11+
var layerArr = []
1112

1213
// Quick hack
1314
var quick_hack = {
@@ -50,6 +51,19 @@ App.map = (function ($, publ) {
5051
// through the template causes encoding problems
5152
publ.updateForm(features);
5253

54+
contents.layers.forEach(function(layer) {
55+
var s = layer.type.split(".");
56+
layerArr.push(
57+
new ol.layer.Tile({
58+
title: layer.name,
59+
baseLayer: true,
60+
visible: false,
61+
source: new ol[s[1]][s[2]](layer.options)
62+
})
63+
);
64+
});
65+
layerArr[0].setVisible(true);
66+
5367
// Layer for vector features
5468
vector = new ol.layer.Vector({
5569
title: "Features",
@@ -61,30 +75,7 @@ App.map = (function ($, publ) {
6175
renderOrder: ol.ordering.yOrdering(),
6276
style: this.getStyle
6377
});
64-
65-
var osm = new ol.layer.Tile({
66-
title: "OSM",
67-
baseLayer: true,
68-
preview: "/plugin_assets/redmine_gtt/images/preview_osm.jpg",
69-
// source: new ol.source.OSM()
70-
source: new ol.source.OSM({
71-
url: "https://tile.openstreetmap.jp/{z}/{x}/{y}.png",
72-
attributions: '<a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
73-
crossOrigin: null,
74-
})
75-
});
76-
77-
var cmj = new ol.layer.Tile({
78-
title: "国土地理院",
79-
baseLayer: true,
80-
preview: "/plugin_assets/redmine_gtt/images/preview_gsi.png",
81-
visible: false,
82-
source: new ol.source.OSM({
83-
url: "https://cyberjapandata.gsi.go.jp/xyz/std/{z}/{x}/{y}.png",
84-
attributions: '<a href="https://portal.cyberjapan.jp/help/termsofuse.html" target="_blank">国土地理院</a>',
85-
crossOrigin: null,
86-
})
87-
});
78+
layerArr.push(vector);
8879

8980
// Layer for project boundary
9081
bounds = new ol.layer.Vector({
@@ -101,6 +92,7 @@ App.map = (function ($, publ) {
10192
})
10293
})
10394
});
95+
layerArr.push(bounds);
10496

10597
// Render project boundary if bounds are available
10698
if (contents.bounds && contents.bounds !== null) {
@@ -112,22 +104,20 @@ App.map = (function ($, publ) {
112104
);
113105
bounds.getSource().addFeature(boundary);
114106

115-
cmj.addFilter(new ol.filter.Mask({
116-
feature: boundary,
117-
inner: false,
118-
fill: new ol.style.Fill({ color:[255,255,255,0.4] })
119-
}));
120-
121-
osm.addFilter(new ol.filter.Mask({
122-
feature: boundary,
123-
inner: false,
124-
fill: new ol.style.Fill({ color:[255,255,255,0.4] })
125-
}));
107+
layerArr.forEach(function(layer) {
108+
if(layer.get("baseLayer")) {
109+
layer.addFilter(new ol.filter.Mask({
110+
feature: boundary,
111+
inner: false,
112+
fill: new ol.style.Fill({ color:[255,255,255,0.4] })
113+
}));
114+
}
115+
});
126116
}
127117

128118
map = new ol.Map({
129119
target: options.target,
130-
layers: [osm,cmj,bounds,vector],
120+
layers: layerArr,
131121
controls: ol.control.defaults({
132122
attributionOptions: ({
133123
collapsible: false
@@ -179,7 +169,7 @@ App.map = (function ($, publ) {
179169
});
180170

181171
// Add LayerSwitcher Image Toolbar
182-
map.addControl(new ol.control.LayerSwitcherImage());
172+
map.addControl(new ol.control.LayerPopup());
183173
};
184174

185175
publ.getColor = function (feature) {

0 commit comments

Comments
 (0)