86d5bcbb2d0c85f3781765713aea5827c6a53e68
[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') {
24                                 if (feature.attributes.condition === undefined) feature.attributes.condition = 0;
25                                 features_sledrun.push(feature);
26                         } else features_point.push(feature);
27                 } else features_path.push(feature);
28         }
29
30
31         // background layer
32         // ----------------
33         var layer_map = new OpenLayers.Layer.Google("Google Physical", {
34                 type: google.maps.MapTypeId.TERRAIN
35         });
36
37         // // Alternative: OSM map
38         // var layer_map = new OpenLayers.Layer.OSM();
39         
40         // // Alternative: Microsoft Bing Maps
41         // var layer_map = new OpenLayers.Layer.Bing({
42         //     type: "Road",
43         //     key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"});
44
45         
46         // path layer
47         // ----------
48         var layer_path = new OpenLayers.Layer.Vector("Path", {
49                 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
50                                 strokeColor: '${getStrokeColor}',
51                                 strokeWidth: '${getStrokeWidth}'
52                         }, {
53                                 context: {
54                                         getStrokeColor: function(feature) {
55                                                 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
56                                                 if (feature.attributes.type == 'sledrun') return '#014e9a';
57                                                 if (feature.attributes.type == 'walk') return '#e98401';
58                                                 if (feature.attributes.type == 'alternative') return '#7f7fff';
59                                                 if (feature.attributes.type == 'lift') return '#000000';
60                                                 return '#ee9900';
61                                         },
62                                         getStrokeWidth: function(feature) {
63                                                 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
64                                                 if (feature.attributes.type == 'lift') return 3;
65                                                 return 6;
66                                         }
67                                 }
68                         }))
69         });
70
71         
72         // point layer
73         // -----------
74         var layer_point = new OpenLayers.Layer.Vector("Point", {
75                 rendererOptions: {yOrdering: true}
76         });
77
78         
79         // sledrun layer
80         // ------------
81         var layer_sledrun = new OpenLayers.Layer.Vector("Sledrun", { 
82                 styleMap: new OpenLayers.StyleMap({
83                         "default": new OpenLayers.Style({
84                                 externalGraphic: "/vorlagen/bahnzustand${condition}_0.png",
85                                 graphicWidth: 17,
86                                 graphicHeight: 17,
87                                 graphicXOffset: -8,
88                                 graphicYOffset: -8,
89                                 graphicZIndex: 12,
90                                 backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png",
91                                 backgroundWidth: 23,
92                                 backgroundHeight: 23,
93                                 backgroundXOffset: -8,
94                                 backgroundYOffset: -8,
95                                 backgroundGraphicZIndex: 11,
96                                 title: "${name}"
97                         }),
98                         "highlight": new OpenLayers.Style({
99                                 label: "${name}",
100                                 labelOutlineColor: "white",
101                                 labelYOffset: 12,
102                                 fontWeight: "bold"
103                         }) 
104                 }),
105                 rendererOptions: {yOrdering: true}
106         });
107
108         // map itself
109         // ----------
110         var lon = parseFloat(jq_map.attr('data-center-lon'));
111         var lat = parseFloat(jq_map.attr('data-center-lat'));
112         var zoom = parseInt(jq_map.attr('data-zoom'));
113         var map = new OpenLayers.Map(jq_map.context, {
114                 projection: EPSG3857,
115                 displayProjection: EPSG4326,
116                 units: "m",
117                 theme: null,
118                 layers: [layer_map, layer_path, layer_point, layer_sledrun],
119                 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
120                 zoom: zoom
121         });
122         
123
124         // add features
125         // if this would be done before the layer is added to the map, the features are not added
126         layer_path.addFeatures(features_path); 
127         layer_point.addFeatures(features_point); 
128         layer_sledrun.addFeatures(features_sledrun); 
129
130         // disable mouse wheel zoom
131         var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
132         navigation_control.disableZoomWheel();
133
134         // layer switcher
135         map.addControl(new OpenLayers.Control.LayerSwitcher());
136
137         // print sledrun name when mouse moves over it
138         map.addControl(new OpenLayers.Control.SelectFeature(layer_sledrun, {
139                 hover: true,
140                 highlightOnly: true,
141                 autoActivate: true,
142                 renderIntent: "highlight"
143         }));
144
145         // show popup when user clicks on a sledrun icon
146         map.addControl(new OpenLayers.Control.SelectFeature(layer_sledrun, {
147                 autoActivate: true,
148                 toggle: true,
149                 onSelect: function(feature) {
150                         // Open popup
151                         var popup_text = "<h2>" + feature.attributes.name + '</h2>\n' +
152                         '<ul>\n' +
153                         '<li><a href="' + feature.attributes.wiki + '">Details zur Rodelbahn</a></li>\n' + 
154                         '<li>Rodelbahnzustand<br/>';
155                         if (feature.attributes.date_report !== undefined) {
156                                 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelm&auml;&szlig;ig', 4: 'Schlecht', 5: 'Geht nicht'};
157                                 var year_month_day = feature.attributes.date_report.split('-');
158                                 popup_text += '<a href="' + feature.attributes.wiki + '#Eintr.C3.A4ge">' + condition_text[feature.attributes.condition] + '</a> ' + 
159                                 '<small>' + year_month_day[2] + '.' + year_month_day[1] + '.</small> ' +
160                                 '<em><a href="' + feature.attributes.wiki + '#Eintragen">Neu</a></em>';
161                         } else {
162                                 popup_text += '<em><a href="' + feature.attributes.wiki + '#Eintragen">Bitte eintragen</a></em>';
163                         }
164                         popup_text += '</li>\n</ul>\n';
165                         var selectFeatureControl = this;
166                         var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + feature.attributes.wiki, 
167                         feature.geometry.getBounds().getCenterLonLat(),
168                         null,
169                         popup_text,
170                         null, true, function(event) {
171                                 // onPopupClose
172                                 selectFeatureControl.unselectAll();
173                         });
174                         feature.popup = popup;
175                         map.addPopup(popup);
176                 },
177                 onUnselect: function(feature) {
178                         // Close popup
179                         map.removePopup(feature.popup);
180                         feature.popup.destroy();
181                         feature.popup = null;
182                 }
183         }));
184 }
185
186
187 function init_wrmaps() {
188         var jq_maps = $('.wrmap'); // all wrmap <div> elements
189         jq_maps.each(init_wrmap);
190 }
191
192
193 $(document).ready(init_wrmaps);
194