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