]> ToastFreeware Gitweb - philipp/winterrodeln/mediawiki_extensions/wrmap.git/blob - wrmap.js
Add shadows below markers.
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.js
1 "use strict";
2 /*
3 // "User defined" popup class to be able to specify a minimum size for the popup,
4 // so that Safari 7.0 displays it correctly (see ticket #89).
5 OpenLayers.Popup.WrInfo = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
6         minSize: new OpenLayers.Size(180, 260),
7
8         initialize: function(id, lonlat, contentSize, contentHTML, anchor, closeBox, closeCallback) {
9                 OpenLayers.Popup.FramedCloud.prototype.initialize.apply(this, arguments);
10         },
11
12         CLASS_NAME: "OpenLayers.Popup.WrInfo"
13 });
14 */
15
16
17 function init_wrmap(i, jq_map) {
18         // define constants
19         var EPSG4326 = ol.proj.get("EPSG:4326"); // lon/lat
20         var EPSG3857 = ol.proj.get("EPSG:3857"); // google
21
22         // tool functions
23         function createElement(tagName, attributes) {
24                 var element = $(document.createElement(tagName));
25                 if (attributes === undefined) return element;
26                 for (var attribute in attributes) {
27                         element.attr(attribute, attributes[attribute]);
28                 }
29                 return element;
30         }
31
32         function appendElement(parentElement, tagName, attributes) {
33                 if (attributes === undefined) attributes = {};
34                 var element = createElement(tagName, attributes);
35                 parentElement.append(element);
36                 return element;
37         }
38
39
40         // extract geojson from map element and clear map element's content
41         jq_map = $(jq_map);
42         var ext_path = jq_map.attr('data-ext-path'); // e.g. '/mediawiki/extensions/wrmap'
43         var img_path = ext_path + '/img';
44         var json_string = jq_map.children().last().text();
45         jq_map.empty(); // once parsed, remove geojson string from the map element.
46         var json_js = JSON.parse(json_string);
47         var format_geojson = new ol.format.GeoJSON();
48         var features_all = format_geojson.readFeatures(json_js, {dataProjection: EPSG4326, featureProjection: EPSG3857});
49
50
51         // background layer
52         // ----------------
53
54         // OSM map
55         var layer_map = new ol.layer.Tile({
56                 source: new ol.source.OSM()
57         });
58
59
60         /*
61         // Microsoft Bing Maps
62         // var layer_map = new OpenLayers.Layer.Bing({
63         //      type: "Road",
64         //      key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA",
65         //      tileOptions: {crossOriginKeyword: null}});
66
67         // // Alternative: Base map
68         // // see: http://www.basemap.at
69         
70         // // Alternative: OpenTopoMap
71         // // see: https://opentopomap.org/about
72
73         // // Alternative: Dummy base layer
74         // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
75         //     isBaseLayer: true});
76         */
77
78
79         // path layer
80         // ----------
81
82         function get_feature_title(feature) {
83                 var title = feature.get('type');
84                 if (title == 'sledrun') return feature.get('name');
85                 title = title.charAt(0).toUpperCase() + title.slice(1); // first letter uppercase
86                 if (feature.get('name')) title += ': ' + feature.get('name');
87                 return title;
88         }
89
90         // Returns 0 to 5 for features that represent sledruns as their condition
91         var get_sledrun_condition = function(feature) {
92                 var condition = feature.get('condition');
93                 if (condition === undefined) return 0;
94                 return condition;
95         }
96
97         function style_point_function(feature, resolution) {
98                 if (feature.get('type') == 'sledrun') {
99                         let condition = get_sledrun_condition(feature);
100                         let src = img_path + '/marker_c_sledrun_' + condition + 'nn.png';
101                         let marker_style = new ol.style.Style({
102                                 image: new ol.style.Icon({
103                                         src: src,
104                                         imgSize: [17, 17],
105                                         anchor: [0.5, 0.5]
106                                 }),
107                         });
108                         let shadow_style = new ol.style.Style({
109                                 image: new ol.style.Icon({
110                                         src: img_path + '/marker_c_shadow.png',
111                                         imgSize: [23, 23],
112                                         anchor: [0.4, 0.4]
113                                 }),
114                         });
115                         return [shadow_style, marker_style];
116
117                 } else {
118                         let src = img_path + '/marker_p_' + feature.get('type') + '.png';
119                         let marker_style = new ol.style.Style({
120                                 image: new ol.style.Icon({
121                                         src: src,
122                                         imgSize: [20, 34],
123                                         anchor: [0.5, 1.0]
124                                 }),
125                         });
126                         return [marker_style];
127                 }
128         }
129
130         function style_point_function_selected(feature, resolution) {
131                 let style_array = style_point_function(feature, resolution);
132                 style_array[0].setText(new ol.style.Text({
133                         text: get_feature_title(feature),
134                         font: 'icon',
135                         offsetY: 14,
136                         stroke: new ol.style.Stroke({
137                                 color: '#ddd',
138                                 width: 2,
139                         }),
140                 }));
141                 return style_array;
142         }
143
144         function style_path_function(feature, resolution) {
145                 var line_color = {
146                         'rodelbahn': '#014e9a',
147                         'gehweg': '#e98401',
148                         'alternative': '#7f7fff',
149                         'lift': '#000000',
150                         'anfahrt': '#e1e100'
151                 };
152                 var color = feature.get('strokeColor') || line_color[feature.get('type')] || '#e7525b';
153                 var width = (feature.get('type') in ['lift', 'anfahrt']) ? 3 : 6;
154                 return new ol.style.Style({
155                         stroke: new ol.style.Stroke({
156                                 color: color,
157                                 width: width
158                         })
159                 });
160         }
161
162         function style_function(feature, resolution) {
163                 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function(feature, resolution);
164                 return style_path_function(feature, resolution);
165         };
166
167         function style_function_selected(feature, resolution) {
168                 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function_selected(feature, resolution);
169                 return style_path_function(feature, resolution);
170         };
171
172
173         // popup overlay
174         // -------------
175         var popup_container = document.getElementById('popup');
176         var popup_content = document.getElementById('popup-content');
177         var popup_closer = document.getElementById('popup-closer');
178         var popup_overlay = new ol.Overlay({element: popup_container, autoPan: {animation: {duration: 250}}});
179         popup_closer.onclick = function() {popup_overlay.setPosition(undefined); popup_closer.blur(); return false;};
180
181         function create_popup_dom(feature) {
182                 var popup_div = createElement('div');
183
184                 // name
185                 if (feature.get('name') !== undefined && (feature.get('wiki') !== undefined || feature.get('thumb_url') !== undefined)) {
186                         var h2 = appendElement(popup_div, 'h2');
187                         if (feature.get('wiki') === undefined) h2.text(feature.get('name'));
188                         else appendElement(h2, 'a', {href: feature.get('wiki')}).text(feature.get('name'));
189                 }
190
191                 // sledrun information
192                 if (feature.get('type') == 'sledrun') {
193                         var p = appendElement(popup_div, 'p').text('Rodelbahnzustand').append(createElement('br'));
194                         if (feature.get('condition') !== undefined) {
195                                 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: 'Geht nicht'};
196                                 var year_month_day = feature.get('date_report').split('-');
197                                 p.append(createElement('a', {href: feature.get('wiki') + '#Eintr.C3.A4ge'}).text(condition_text[feature.get('condition')]), ' ');
198                                 p.append(createElement('small').text(year_month_day[2] + '.' + year_month_day[1] + '.'), ' ');
199                                 p.append(createElement('em').append(createElement('a', {href: feature.get('wiki') + '#Eintragen'}).text('Neu')));
200                         } else {
201                                 p.append(createElement('em').append(createElement('a', {href: feature.get('wiki') + '#Eintragen'}).text('Bitte eintragen')));
202                         }
203                 }
204
205                 // wiki link
206                 if (feature.get('wiki') !== undefined) {
207                         var a = appendElement(appendElement(popup_div, 'p'), 'a', {href: feature.get('wiki')});
208                         var detail_text = 'Details';
209                         if (feature.get('type') == 'sledrun') detail_text += ' zur Rodelbahn';
210                         if (feature.get('type') == 'gasthaus') detail_text += ' zum Gasthaus';
211                         if (feature.get('thumb_url') === undefined) a.text(detail_text);
212                         else a.append(createElement('img', {src: feature.get('thumb_url'), alt: detail_text, title: detail_text}));
213                 }
214
215                 return popup_div;
216         }
217
218
219         /*
220         // point layer
221         // -----------
222         var filter_point_sledrun = new OpenLayers.Filter.Comparison({
223                 type: OpenLayers.Filter.Comparison.EQUAL_TO,
224                 property: 'type',
225                 value: 'sledrun'
226         });
227
228
229         var layer_point = new OpenLayers.Layer.Vector("Point", {
230                 styleMap: new OpenLayers.StyleMap({
231                         'default': new OpenLayers.Style({
232                                         graphicZIndex: 12,
233                                         backgroundGraphicZIndex: 11,
234                                 }, {
235                                         context: {
236                                                 // the following context functions should only be available in the rule that uses them,
237                                                 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
238                                                 getCondition: get_sledrun_condition,
239                                                 getSymbol: function(feature) {
240                                                         var name = feature.attributes.type;
241                                                         return name;
242                                                 }
243                                         },
244                                         rules: [
245                                                 new OpenLayers.Rule({
246                                                         filter: filter_point_sledrun,
247                                                         symbolizer: {
248                                                                 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nn.png',
249                                                                 graphicWidth: 17,
250                                                                 graphicHeight: 17,
251                                                                 graphicXOffset: -8,
252                                                                 graphicYOffset: -8,
253                                                                 backgroundGraphic: img_path + '/marker_c_shadow.png',
254                                                                 backgroundWidth: 23,
255                                                                 backgroundHeight: 23,
256                                                                 backgroundXOffset: -8,
257                                                                 backgroundYOffset: -8,
258                                                         }
259                                                 }),
260                                                 new OpenLayers.Rule({
261                                                         elseFilter: true,
262                                                         symbolizer: {
263                                                                 externalGraphic: img_path + '/marker_p_${getSymbol}.png',
264                                                                 graphicWidth: 20,
265                                                                 graphicHeight: 34,
266                                                                 graphicXOffset: -10,
267                                                                 graphicYOffset: -33,
268                                                         }
269                                                 })
270                                         ]
271                                 }),
272                         highlight: new OpenLayers.Style({
273                                         label: "${getTitle}",
274                                         labelOutlineColor: "white",
275                                         fontWeight: "bold"
276                                 }, {
277                                         context: {
278                                                 getCondition: get_sledrun_condition,
279                                                 getTitle: function(feature) {
280                                                         var title = '';
281                                                         if (feature.attributes.type != 'point') {
282                                                                 title = feature.attributes.type;
283                                                                 title = title.charAt(0).toUpperCase() + title.slice(1); // First letter uppercase
284                                                                 if (feature.attributes.name !== undefined) title += ': ';
285                                                         }
286                                                         if (feature.attributes.name !== undefined) title += feature.attributes.name;
287                                                         return title;
288                                                 }
289                                         },
290                                         rules: [
291                                                 new OpenLayers.Rule({
292                                                         filter: filter_point_sledrun,
293                                                         symbolizer: {
294                                                                 label: "${name}",
295                                                                 labelYOffset: 14,
296                                                                 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png'
297                                                         }
298                                                 }),
299                                                 new OpenLayers.Rule({
300                                                         elseFilter: true,
301                                                         symbolizer: {
302                                                                 labelYOffset: 40
303                                                         }
304                                                 })
305                                         ]
306                                 }),
307                         select: new OpenLayers.Style({
308                                         fillOpacity: 1.
309                                 }, {
310                                         context: {
311                                                 getCondition: get_sledrun_condition,
312                                         },
313                                         rules: [
314                                                 new OpenLayers.Rule({
315                                                         filter: filter_point_sledrun,
316                                                         symbolizer: {
317                                                                 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png',
318                                                                 backgroundGraphic: false,
319                                                                 graphicXOffset: -6,
320                                                                 graphicYOffset: -6
321                                                         }
322                                                 }),
323                                                 new OpenLayers.Rule({
324                                                         elseFilter: true
325                                                 })
326                                         ]
327                                 })
328                 }),
329                 rendererOptions: {yOrdering: true}
330         });
331
332 */
333         // map itself
334         // ----------
335         var lon = json_js.properties.lon;
336         var lat = json_js.properties.lat;
337         var zoom = json_js.properties.zoom;
338         var width = json_js.properties.width;
339         var height = json_js.properties.height;
340         if (zoom === undefined) zoom = 10; // default zoom
341         if (width === undefined) width = '100%'; // default width
342         if (height === undefined) height = 450;  // default: 450 pixel
343         jq_map.width(width);
344         jq_map.height(height);
345
346         var layer_sledrun_source = new ol.source.Vector({features: features_all});
347         var layer_sledrun = new ol.layer.Vector({
348                 source: layer_sledrun_source,
349                 style: style_function
350         });
351
352         var map = new ol.Map({
353                 target: jq_map[0],
354                 layers: [
355                         layer_map,
356                         layer_sledrun
357                 ],
358                 overlays: [popup_overlay],
359                 view: new ol.View({
360                         center: ol.proj.fromLonLat([lon, lat]),
361                         zoom: zoom
362                 }),
363                 controls: ol.control.defaults({
364                         attributionOptions: {
365                                 collapsible: false
366                         }
367                 }),
368                 interactions: ol.interaction.defaults({
369                         mouseWheelZoom: false
370                 })
371         });
372
373
374         var select_hover = new ol.interaction.Select({
375                 condition: ol.events.condition.pointerMove,
376                 style: style_function_selected,
377         });
378         map.addInteraction(select_hover);
379
380         var select_click = new ol.interaction.Select({
381                 condition: ol.events.condition.click,
382         });
383         map.addInteraction(select_click);
384         select_click.on('select', function(event) {
385                 if (event.selected.length > 0) {
386                         let feature = event.selected[0];
387                         let coordinates = feature.getGeometry().getCoordinates();
388                         let popup_dom = create_popup_dom(feature);
389                         if (popup_dom.children().length > 0) {
390                                 $(popup_content).empty().append(popup_dom);
391                                 popup_overlay.setPosition(coordinates);
392                         }
393                 }
394         });
395 }
396
397 function init_wrmaps() {
398         var jq_maps = $('.wrmap'); // all wrmap <div> elements
399         jq_maps.each(init_wrmap);
400 }
401
402
403 $(document).ready(init_wrmaps);
404