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