c1eab9621372028ff5567e0119f511a62aef0d7a
[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 format_json = new OpenLayers.Format.GeoJSON();
15         var features_all = format_json.read(json_string);
16         var features_path = new Array();
17         var features_point = new Array();
18         var features_sledrun = 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.CLASS_NAME == 'OpenLayers.Geometry.Point') {
23                         if (feature.attributes.type == 'sledrun') features_sledrun.push(feature);
24                         else 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 layer_point = new OpenLayers.Layer.Vector("Point", {
73                 rendererOptions: {yOrdering: true}
74         });
75
76         
77         // sledrun layer
78         // ------------
79         var layer_sledrun = new OpenLayers.Layer.Vector("Sledrun", { 
80                 styleMap: new OpenLayers.StyleMap({
81                         "default": new OpenLayers.Style({
82                                 externalGraphic: "/vorlagen/bahnzustand${condition}_0.png",
83                                 graphicWidth: 17,
84                                 graphicHeight: 17,
85                                 graphicXOffset: -8,
86                                 graphicYOffset: -8,
87                                 graphicZIndex: 12,
88                                 backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png",
89                                 backgroundWidth: 23,
90                                 backgroundHeight: 23,
91                                 backgroundXOffset: -8,
92                                 backgroundYOffset: -8,
93                                 backgroundGraphicZIndex: 11,
94                                 title: "${name}"
95                         }),
96                         "highlight": new OpenLayers.Style({
97                                 label: "${name}",
98                                 labelOutlineColor: "white",
99                                 labelYOffset: 12,
100                                 fontWeight: "bold"
101                         }) 
102                 }),
103                 rendererOptions: {yOrdering: true}
104         });
105
106         // map itself
107         // ----------
108         var lon = parseFloat(jq_map.attr('data-center-lon'));
109         var lat = parseFloat(jq_map.attr('data-center-lat'));
110         var zoom = parseInt(jq_map.attr('data-zoom'));
111         var map = new OpenLayers.Map(jq_map.context, {
112                 projection: EPSG3857,
113                 displayProjection: EPSG4326,
114                 units: "m",
115                 theme: null,
116                 layers: [layer_map, layer_path, layer_point, layer_sledrun],
117                 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
118                 zoom: zoom
119         });
120         
121
122         // add features
123         // if this would be done before the layer is added to the map, the features are not added
124         layer_path.addFeatures(features_path); 
125         layer_point.addFeatures(features_point); 
126         layer_sledrun.addFeatures(features_sledrun); 
127
128         // disable mouse wheel zoom
129         var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
130         navigation_control.disableZoomWheel();
131
132         // layer switcher
133         map.addControl(new OpenLayers.Control.LayerSwitcher());
134
135         // print sledrun name when mouse moves over it
136         map.addControl(new OpenLayers.Control.SelectFeature(layer_sledrun, {
137                 hover: true,
138                 highlightOnly: true,
139                 autoActivate: true,
140                 renderIntent: "highlight"
141         }));
142
143         // show popup when user clicks on a sledrun icon
144         map.addControl(new OpenLayers.Control.SelectFeature(layer_sledrun, {
145                 autoActivate: true,
146                 toggle: true,
147                 onSelect: function(feature) {
148                         // Open popup
149                         var popup_text = "<h2>" + feature.attributes.name + '</h2>\n' +
150                         '<ul>\n' +
151                         '<li><a href="' + feature.attributes.wiki + '">Details zur Rodelbahn</a></li>\n' + 
152                         '<li>Rodelbahnzustand<br/>';
153                         if (feature.attributes.date_report !== undefined) {
154                                 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelm&auml;&szlig;ig', 4: 'Schlecht', 5: 'Geht nicht'};
155                                 var year_month_day = feature.attributes.date_report.split('-');
156                                 popup_text += '<a href="' + feature.attributes.wiki + '#Eintr.C3.A4ge">' + condition_text[feature.attributes.condition] + '</a> ' + 
157                                 '<small>' + year_month_day[2] + '.' + year_month_day[1] + '.</small> ' +
158                                 '<em><a href="' + feature.attributes.wiki + '#Eintragen">Neu</a></em>';
159                         } else {
160                                 popup_text += '<em><a href="' + feature.attributes.wiki + '#Eintragen">Bitte eintragen</a></em>';
161                         }
162                         popup_text += '</li>\n</ul>\n';
163                         var selectFeatureControl = this;
164                         var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + feature.attributes.wiki, 
165                         feature.geometry.getBounds().getCenterLonLat(),
166                         null,
167                         popup_text,
168                         null, true, function(event) {
169                                 // onPopupClose
170                                 selectFeatureControl.unselectAll();
171                         });
172                         feature.popup = popup;
173                         map.addPopup(popup);
174                 },
175                 onUnselect: function(feature) {
176                         // Close popup
177                         map.removePopup(feature.popup);
178                         feature.popup.destroy();
179                         feature.popup = null;
180                 }
181         }));
182 }
183
184
185 function init_wrmaps() {
186         var jq_maps = $('.wrmap'); // all wrmap <div> elements
187         jq_maps.each(init_wrmap);
188 }
189
190
191 $(document).ready(init_wrmaps);
192