3 function init_wrmap(i, 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.
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);
28 var layer_map = new OpenLayers.Layer.Google("Google Physical", {
29 type: google.maps.MapTypeId.TERRAIN
32 // // Alternative: OSM map
33 // var layer_map = new OpenLayers.Layer.OSM();
35 // // Alternative: Microsoft Bing Maps
36 // var layer_map = new OpenLayers.Layer.Bing({
38 // key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"});
40 // // Alternative: Dummy base layer
41 // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
42 // isBaseLayer: true});
47 var layer_path = new OpenLayers.Layer.Vector("Path", {
48 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
49 strokeColor: '${getStrokeColor}',
50 strokeWidth: '${getStrokeWidth}'
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';
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;
74 var filter_point_sledrun = new OpenLayers.Filter.Comparison({
75 type: OpenLayers.Filter.Comparison.EQUAL_TO,
80 var layer_point = new OpenLayers.Layer.Vector("Point", {
81 styleMap: new OpenLayers.StyleMap({
82 'default': new OpenLayers.Style({
84 backgroundGraphicZIndex: 11,
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;
93 getSymbol: function(feature) {
94 var name = feature.attributes.type;
100 filter: filter_point_sledrun,
102 externalGraphic: '/vorlagen/bahnzustand${getCondition}_0.png',
107 backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png",
109 backgroundHeight: 23,
110 backgroundXOffset: -8,
111 backgroundYOffset: -8,
114 new OpenLayers.Rule({
117 externalGraphic: '/vorlagen/gmap_${getSymbol}.png',
126 highlight: new OpenLayers.Style({
127 label: "${getTitle}",
128 labelOutlineColor: "white",
132 getTitle: function(feature) {
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 += ': ';
139 if (feature.attributes.name !== undefined) title += feature.attributes.name;
144 new OpenLayers.Rule({
145 filter: filter_point_sledrun,
151 new OpenLayers.Rule({
160 rendererOptions: {yOrdering: true}
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,
175 layers: [layer_map, layer_path, layer_point],
176 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
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);
186 // disable mouse wheel zoom
187 var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
188 navigation_control.disableZoomWheel();
191 // map.addControl(new OpenLayers.Control.LayerSwitcher());
193 // print sledrun name when mouse moves over it
194 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
198 renderIntent: "highlight"
201 // show popup when user clicks on a sledrun icon
202 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
205 onSelect: function(feature) {
207 if (feature.attributes.type == 'sledrun') {
208 popup_text += "<h2>" + feature.attributes.name + '</h2>\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äß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>';
219 popup_text += '<em><a href="' + feature.attributes.wiki + '#Eintragen">Bitte eintragen</a></em>';
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';
228 var selectFeatureControl = this;
229 var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + feature.attributes.wiki,
230 feature.geometry.getBounds().getCenterLonLat(),
233 null, true, function(event) {
235 selectFeatureControl.unselectAll();
237 feature.popup = popup;
240 onUnselect: function(feature) {
241 if (feature.popup === null) return;
244 map.removePopup(feature.popup);
245 feature.popup.destroy();
246 feature.popup = null;
252 function init_wrmaps() {
253 var jq_maps = $('.wrmap'); // all wrmap <div> elements
254 jq_maps.each(init_wrmap);
258 $(document).ready(init_wrmaps);