3 function init_wrmap(i, jq_map) {
5 var EPSG4326 = new OpenLayers.Projection("EPSG:4326"); // lon/lat
6 var EPSG3857 = new OpenLayers.Projection("EPSG:3857"); // google
9 function createElement(tagName, attributes={}) {
10 var element = $(document.createElement(tagName));
11 for (var attribute in attributes) {
12 element.attr(attribute, attributes[attribute]);
17 function appendElement(parentElement, tagName, attributes={}) {
18 var element = createElement(tagName, attributes);
19 parentElement.append(element);
24 // extract geojson from map element and clear map element's content
26 OpenLayers.ImgPath = jq_map.attr('data-img-path'); // e.g. "/mediawiki/extensions/wrmap/openlayers/img/"
27 var json_string = jq_map.children().last().text();
28 jq_map.empty(); // once parsed, remove geojson string from the map element.
29 var format_json = new OpenLayers.Format.JSON();
30 var json_js = format_json.read(json_string);
31 var format_geojson = new OpenLayers.Format.GeoJSON();
32 var features_all = format_geojson.read(json_js);
34 // extract, tranform and split features to layers
35 var features_path = new Array();
36 var features_point = new Array();
37 for (var i = 0; i != features_all.length; ++i) {
38 var feature = features_all[i];
39 feature.geometry.transform(EPSG4326, EPSG3857);
40 if (feature.geometry instanceof OpenLayers.Geometry.Point) features_point.push(feature);
41 else features_path.push(feature);
47 var layer_map = new OpenLayers.Layer.Google("Google Physical", {
48 type: google.maps.MapTypeId.TERRAIN
51 // // Alternative: OSM map
52 // var layer_map = new OpenLayers.Layer.OSM();
54 // // Alternative: Microsoft Bing Maps
55 // var layer_map = new OpenLayers.Layer.Bing({
57 // key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"});
59 // // Alternative: Dummy base layer
60 // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
61 // isBaseLayer: true});
66 var layer_path = new OpenLayers.Layer.Vector("Path", {
67 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
68 strokeColor: '${getStrokeColor}',
69 strokeWidth: '${getStrokeWidth}'
72 getStrokeColor: function(feature) {
73 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
74 if (feature.attributes.type == 'rodelbahn') return '#014e9a';
75 if (feature.attributes.type == 'gehweg') return '#e98401';
76 if (feature.attributes.type == 'alternative') return '#7f7fff';
77 if (feature.attributes.type == 'lift') return '#000000';
78 if (feature.attributes.type == 'anfahrt') return '#e1e100';
81 getStrokeWidth: function(feature) {
82 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
83 if (feature.attributes.type == 'lift' || feature.attributes.type == 'anfahrt') return 3;
93 var filter_point_sledrun = new OpenLayers.Filter.Comparison({
94 type: OpenLayers.Filter.Comparison.EQUAL_TO,
99 var layer_point = new OpenLayers.Layer.Vector("Point", {
100 styleMap: new OpenLayers.StyleMap({
101 'default': new OpenLayers.Style({
103 backgroundGraphicZIndex: 11,
106 // the following context functions should only be available in the rule that uses them,
107 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
108 getCondition: function(feature) {
109 if (feature.attributes.condition === undefined) return 0;
110 return feature.attributes.condition;
112 getSymbol: function(feature) {
113 var name = feature.attributes.type;
118 new OpenLayers.Rule({
119 filter: filter_point_sledrun,
121 externalGraphic: '/vorlagen/bahnzustand${getCondition}_0.png',
126 backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png",
128 backgroundHeight: 23,
129 backgroundXOffset: -8,
130 backgroundYOffset: -8,
133 new OpenLayers.Rule({
136 externalGraphic: '/vorlagen/gmap_${getSymbol}.png',
145 highlight: new OpenLayers.Style({
146 label: "${getTitle}",
147 labelOutlineColor: "white",
151 getTitle: function(feature) {
153 if (feature.attributes.type != 'point') {
154 title = feature.attributes.type;
155 title = title.charAt(0).toUpperCase() + title.slice(1); // First letter uppercase
156 if (feature.attributes.name !== undefined) title += ': ';
158 if (feature.attributes.name !== undefined) title += feature.attributes.name;
163 new OpenLayers.Rule({
164 filter: filter_point_sledrun,
170 new OpenLayers.Rule({
178 select: new OpenLayers.Style({
182 rendererOptions: {yOrdering: true}
188 var lon = json_js.properties.lon;
189 var lat = json_js.properties.lat;
190 var zoom = json_js.properties.zoom;
191 var width = json_js.properties.width;
192 var height = json_js.properties.height;
193 if (zoom === undefined) zoom = 10; // default zoom
194 if (width === undefined) width = '100%'; // default width
195 if (height === undefined) height = 450; // default: 450 pixel
197 jq_map.height(height);
198 var map = new OpenLayers.Map(jq_map.context, {
199 projection: EPSG3857,
200 displayProjection: EPSG4326,
203 layers: [layer_map, layer_path, layer_point],
204 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
210 // if this would be done before the layer is added to the map, the features are not added
211 layer_path.addFeatures(features_path);
212 layer_point.addFeatures(features_point);
214 // disable mouse wheel zoom
215 var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
216 navigation_control.disableZoomWheel();
219 // map.addControl(new OpenLayers.Control.LayerSwitcher());
221 // print sledrun name when mouse moves over it
222 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
226 renderIntent: "highlight"
229 // show popup when user clicks on a sledrun icon
230 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
233 onSelect: function(feature) {
234 var attr = feature.attributes;
235 var popup_div = createElement('div');
238 if (attr.name !== undefined && (attr.wiki !== undefined || attr.thumb_url !== undefined)) {
239 var h2 = appendElement(popup_div, 'h2');
240 if (attr.wiki === undefined) h2.text(attr.name);
241 else appendElement(h2, 'a', {href: attr.wiki}).text(attr.name);
244 // sledrun information
245 if (attr.type == 'sledrun') {
246 var p = appendElement(popup_div, 'p').text('Rodelbahnzustand').append(createElement('br'));
247 if (attr.condition !== undefined) {
248 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: 'Geht nicht'};
249 var year_month_day = attr.date_report.split('-');
250 p.append(createElement('a', {href: attr.wiki + '#Eintr.C3.A4ge'}).text(condition_text[attr.condition]), ' ');
251 p.append(createElement('small').text(year_month_day[2] + '.' + year_month_day[1] + '.'), ' ');
252 p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Neu')));
254 p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Bitte eintragen')));
259 if (attr.wiki !== undefined) {
260 var a = appendElement(appendElement(popup_div, 'p'), 'a', {href: attr.wiki});
261 var detail_text = 'Details';
262 if (attr.type == 'sledrun') detail_text += ' zur Rodelbahn';
263 if (attr.type == 'gasthaus') detail_text += ' zum Gasthaus';
264 if (attr.thumb_url === undefined) a.text(detail_text);
265 else a.append(createElement('img', {src: attr.thumb_url, alt: detail_text, title: detail_text}));
268 // no popup if we don't have anything to say
269 if (popup_div.children().length == 0) return;
272 var selectFeatureControl = this;
273 var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + attr.wiki,
274 feature.geometry.getBounds().getCenterLonLat(),
277 null, true, function(event) {
279 selectFeatureControl.unselectAll();
281 feature.popup = popup;
284 onUnselect: function(feature) {
285 if (feature.popup === null) return;
288 map.removePopup(feature.popup);
289 feature.popup.destroy();
290 feature.popup = null;
296 function init_wrmaps() {
297 var jq_maps = $('.wrmap'); // all wrmap <div> elements
298 jq_maps.each(init_wrmap);
302 $(document).ready(init_wrmaps);