]> ToastFreeware Gitweb - philipp/winterrodeln/mediawiki_extensions/wrmap.git/blob - wrmap.js
Tagged output with 'nowiki' to prevent further processing.
[philipp/winterrodeln/mediawiki_extensions/wrmap.git] / wrmap.js
1 "use strict";
2
3 function init_wrmap(i, jq_map) {
4         // define constants
5         var EPSG4326 = new OpenLayers.Projection("EPSG:4326"); // lon/lat
6         var EPSG3857 = new OpenLayers.Projection("EPSG:3857"); // google
7
8         // extract geojson from map element and clear map element's content
9         jq_map = $(jq_map);
10         OpenLayers.ImgPath = jq_map.attr('data-img-path'); // e.g. "/mediawiki/extensions/wrmap/openlayers/img/"
11         var json_string = jq_map.text();
12         jq_map.empty(); // once parsed, remove geojson string from the map element.
13         var format_json = new OpenLayers.Format.JSON();
14         var json_js = format_json.read(json_string);
15         var format_geojson = new OpenLayers.Format.GeoJSON();
16         var features_all = format_geojson.read(json_js);
17
18         // extract, tranform and split features to layers
19         var features_path = new Array();
20         var features_point = new Array();
21         for (var i = 0; i != features_all.length; ++i) {
22                 var feature = features_all[i];
23                 feature.geometry.transform(EPSG4326, EPSG3857);
24                 if (feature.geometry instanceof OpenLayers.Geometry.Point) features_point.push(feature);
25                 else features_path.push(feature);
26         }
27
28
29         // background layer
30         // ----------------
31         var layer_map = new OpenLayers.Layer.Google("Google Physical", {
32                 type: google.maps.MapTypeId.TERRAIN
33         });
34
35         // // Alternative: OSM map
36         // var layer_map = new OpenLayers.Layer.OSM();
37         
38         // // Alternative: Microsoft Bing Maps
39         // var layer_map = new OpenLayers.Layer.Bing({
40         //     type: "Road",
41         //     key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"});
42         
43         // // Alternative: Dummy base layer
44         // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
45         //     isBaseLayer: true});
46
47         
48         // path layer
49         // ----------
50         var layer_path = new OpenLayers.Layer.Vector("Path", {
51                 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
52                                 strokeColor: '${getStrokeColor}',
53                                 strokeWidth: '${getStrokeWidth}'
54                         }, {
55                                 context: {
56                                         getStrokeColor: function(feature) {
57                                                 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
58                                                 if (feature.attributes.type == 'rodelbahn') return '#014e9a';
59                                                 if (feature.attributes.type == 'gehweg') return '#e98401';
60                                                 if (feature.attributes.type == 'alternative') return '#7f7fff';
61                                                 if (feature.attributes.type == 'lift') return '#000000';
62                                                 if (feature.attributes.type == 'anfahrt') return '#e1e100';
63                                                 return '#ee9900';
64                                         },
65                                         getStrokeWidth: function(feature) {
66                                                 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
67                                                 if (feature.attributes.type == 'lift' || feature.attributes.type == 'anfahrt') return 3;
68                                                 return 6;
69                                         }
70                                 }
71                         }))
72         });
73
74         
75         // point layer
76         // -----------
77         var filter_point_sledrun = new OpenLayers.Filter.Comparison({
78                 type: OpenLayers.Filter.Comparison.EQUAL_TO,
79                 property: 'type',
80                 value: 'sledrun'
81         });
82
83         var layer_point = new OpenLayers.Layer.Vector("Point", {
84                 styleMap: new OpenLayers.StyleMap({
85                         'default': new OpenLayers.Style({
86                                         graphicZIndex: 12,
87                                         backgroundGraphicZIndex: 11,
88                                 }, {
89                                         context: {
90                                                 // the following context functions should only be available in the rule that uses them,
91                                                 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
92                                                 getCondition: function(feature) {
93                                                         if (feature.condition === undefined) return 0;
94                                                         return feature.condition;
95                                                 },
96                                                 getSymbol: function(feature) {
97                                                         var name = feature.attributes.type;
98                                                         return name;
99                                                 }
100                                         },
101                                         rules: [
102                                                 new OpenLayers.Rule({
103                                                         filter: filter_point_sledrun,
104                                                         symbolizer: {
105                                                                 externalGraphic: '/vorlagen/bahnzustand${getCondition}_0.png',
106                                                                 graphicWidth: 17,
107                                                                 graphicHeight: 17,
108                                                                 graphicXOffset: -8,
109                                                                 graphicYOffset: -8,
110                                                                 backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png",
111                                                                 backgroundWidth: 23,
112                                                                 backgroundHeight: 23,
113                                                                 backgroundXOffset: -8,
114                                                                 backgroundYOffset: -8,
115                                                         }
116                                                 }),
117                                                 new OpenLayers.Rule({
118                                                         elseFilter: true,
119                                                         symbolizer: {
120                                                                 externalGraphic: '/vorlagen/gmap_${getSymbol}.png',
121                                                                 graphicWidth: 20,
122                                                                 graphicHeight: 34,
123                                                                 graphicXOffset: -10,
124                                                                 graphicYOffset: -33,
125                                                         }
126                                                 })
127                                         ]
128                                 }),
129                         highlight: new OpenLayers.Style({
130                                         label: "${getTitle}",
131                                         labelOutlineColor: "white",
132                                         fontWeight: "bold"
133                                 }, {
134                                         context: {
135                                                 getTitle: function(feature) {
136                                                         var title = '';
137                                                         if (feature.attributes.type != 'point') {
138                                                                 title = feature.attributes.type;
139                                                                 title = title.charAt(0).toUpperCase() + title.slice(1); // First letter uppercase
140                                                                 if (feature.attributes.name !== undefined) title += ': ';
141                                                         }
142                                                         if (feature.attributes.name !== undefined) title += feature.attributes.name;
143                                                         return title;
144                                                 }
145                                         },
146                                         rules: [
147                                                 new OpenLayers.Rule({
148                                                         filter: filter_point_sledrun,
149                                                         symbolizer: {
150                                                                 label: "${name}",
151                                                                 labelYOffset: 14
152                                                         }
153                                                 }),
154                                                 new OpenLayers.Rule({
155                                                         elseFilter: true,
156                                                         symbolizer: {
157                                                                 labelYOffset: 40
158                                                         }
159                                                 })
160                                         ]
161                                 }) 
162                 }),
163                 rendererOptions: {yOrdering: true}
164         });
165
166
167         // map itself
168         // ----------
169         var lon = json_js.properties.lon;
170         var lat = json_js.properties.lat;
171         var zoom = json_js.properties.zoom;
172         var width = json_js.properties.width;
173         var height = json_js.properties.height;
174         if (zoom === undefined) zoom = 10; // default zoom
175         if (width === undefined) width = '100%'; // default width
176         if (height === undefined) height = 450;  // default: 450 pixel
177         jq_map.width(width);
178         jq_map.height(height);
179         var map = new OpenLayers.Map(jq_map.context, {
180                 projection: EPSG3857,
181                 displayProjection: EPSG4326,
182                 units: "m",
183                 theme: null,
184                 layers: [layer_map, layer_path, layer_point],
185                 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
186                 zoom: zoom
187         });
188         
189
190         // add features
191         // if this would be done before the layer is added to the map, the features are not added
192         layer_path.addFeatures(features_path); 
193         layer_point.addFeatures(features_point); 
194
195         // disable mouse wheel zoom
196         var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
197         navigation_control.disableZoomWheel();
198
199         // layer switcher
200         // map.addControl(new OpenLayers.Control.LayerSwitcher());
201
202         // print sledrun name when mouse moves over it
203         map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
204                 hover: true,
205                 highlightOnly: true,
206                 autoActivate: true,
207                 renderIntent: "highlight"
208         }));
209
210         // show popup when user clicks on a sledrun icon
211         map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
212                 autoActivate: true,
213                 toggle: true,
214                 onSelect: function(feature) {
215                         var popup_text = '';
216                         if (feature.attributes.type == 'sledrun') {
217                                 popup_text += "<h2>" + feature.attributes.name + '</h2>\n' +
218                                 '<ul>\n' +
219                                 '<li><a href="' + feature.attributes.wiki + '">Details zur Rodelbahn</a></li>\n' + 
220                                 '<li>Rodelbahnzustand<br/>';
221                                 if (feature.attributes.condition !== undefined) {
222                                         var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelm&auml;&szlig;ig', 4: 'Schlecht', 5: 'Geht nicht'};
223                                         var year_month_day = feature.attributes.date_report.split('-');
224                                         popup_text += '<a href="' + feature.attributes.wiki + '#Eintr.C3.A4ge">' + condition_text[feature.attributes.condition] + '</a> ' + 
225                                         '<small>' + year_month_day[2] + '.' + year_month_day[1] + '.</small> ' +
226                                         '<em><a href="' + feature.attributes.wiki + '#Eintragen">Neu</a></em>';
227                                 } else {
228                                         popup_text += '<em><a href="' + feature.attributes.wiki + '#Eintragen">Bitte eintragen</a></em>';
229                                 }
230                                 popup_text += '</li>\n</ul>\n';
231                         } else if (feature.attributes.wiki !== undefined) {
232                                 if (feature.attributes.name != undefined) popup_text += '<h2>' + feature.attributes.name + '</h2>\n';
233                                 popup_text += '<p><a href="' + feature.attributes.wiki + '">Details</a></p>\n';
234                         } else return;
235
236                         // Open popup
237                         var selectFeatureControl = this;
238                         var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + feature.attributes.wiki, 
239                         feature.geometry.getBounds().getCenterLonLat(),
240                         null,
241                         popup_text,
242                         null, true, function(event) {
243                                 // onPopupClose
244                                 selectFeatureControl.unselectAll();
245                         });
246                         feature.popup = popup;
247                         map.addPopup(popup);
248                 },
249                 onUnselect: function(feature) {
250                         if (feature.popup === null) return;
251
252                         // Close popup
253                         map.removePopup(feature.popup);
254                         feature.popup.destroy();
255                         feature.popup = null;
256                 }
257         }));
258 }
259
260
261 function init_wrmaps() {
262         var jq_maps = $('.wrmap'); // all wrmap <div> elements
263         jq_maps.each(init_wrmap);
264 }
265
266
267 $(document).ready(init_wrmaps);
268