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