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