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
8 // extract geojson from map element and clear map element's content
10 OpenLayers.ImgPath = jq_map.attr('data-img-path'); // e.g. "/mediawiki/extensions/wrmap/openlayers/img/"
11 var json_string = jq_map.children().last().text();
12 jq_map.empty(); // once parsed, remove geojson string from the map element.
13 var format_json = new OpenLayers.Format.JSON();
14 var json_js = format_json.read(json_string);
15 var format_geojson = new OpenLayers.Format.GeoJSON();
16 var features_all = format_geojson.read(json_js);
18 // extract, tranform and split features to layers
19 var features_path = new Array();
20 var features_point = new Array();
21 for (var i = 0; i != features_all.length; ++i) {
22 var feature = features_all[i];
23 feature.geometry.transform(EPSG4326, EPSG3857);
24 if (feature.geometry instanceof OpenLayers.Geometry.Point) features_point.push(feature);
25 else features_path.push(feature);
31 var layer_map = new OpenLayers.Layer.Google("Google Physical", {
32 type: google.maps.MapTypeId.TERRAIN
35 // // Alternative: OSM map
36 // var layer_map = new OpenLayers.Layer.OSM();
38 // // Alternative: Microsoft Bing Maps
39 // var layer_map = new OpenLayers.Layer.Bing({
41 // key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"});
43 // // Alternative: Dummy base layer
44 // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
45 // isBaseLayer: true});
50 var layer_path = new OpenLayers.Layer.Vector("Path", {
51 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
52 strokeColor: '${getStrokeColor}',
53 strokeWidth: '${getStrokeWidth}'
56 getStrokeColor: function(feature) {
57 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
58 if (feature.attributes.type == 'rodelbahn') return '#014e9a';
59 if (feature.attributes.type == 'gehweg') return '#e98401';
60 if (feature.attributes.type == 'alternative') return '#7f7fff';
61 if (feature.attributes.type == 'lift') return '#000000';
62 if (feature.attributes.type == 'anfahrt') return '#e1e100';
65 getStrokeWidth: function(feature) {
66 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
67 if (feature.attributes.type == 'lift' || feature.attributes.type == 'anfahrt') return 3;
77 var filter_point_sledrun = new OpenLayers.Filter.Comparison({
78 type: OpenLayers.Filter.Comparison.EQUAL_TO,
83 var layer_point = new OpenLayers.Layer.Vector("Point", {
84 styleMap: new OpenLayers.StyleMap({
85 'default': new OpenLayers.Style({
87 backgroundGraphicZIndex: 11,
90 // the following context functions should only be available in the rule that uses them,
91 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
92 getCondition: function(feature) {
93 if (feature.attributes.condition === undefined) return 0;
94 return feature.attributes.condition;
96 getSymbol: function(feature) {
97 var name = feature.attributes.type;
102 new OpenLayers.Rule({
103 filter: filter_point_sledrun,
105 externalGraphic: '/vorlagen/bahnzustand${getCondition}_0.png',
110 backgroundGraphic: "/vorlagen/gmap_rodelbahn_c_s.png",
112 backgroundHeight: 23,
113 backgroundXOffset: -8,
114 backgroundYOffset: -8,
117 new OpenLayers.Rule({
120 externalGraphic: '/vorlagen/gmap_${getSymbol}.png',
129 highlight: new OpenLayers.Style({
130 label: "${getTitle}",
131 labelOutlineColor: "white",
135 getTitle: function(feature) {
137 if (feature.attributes.type != 'point') {
138 title = feature.attributes.type;
139 title = title.charAt(0).toUpperCase() + title.slice(1); // First letter uppercase
140 if (feature.attributes.name !== undefined) title += ': ';
142 if (feature.attributes.name !== undefined) title += feature.attributes.name;
147 new OpenLayers.Rule({
148 filter: filter_point_sledrun,
154 new OpenLayers.Rule({
163 rendererOptions: {yOrdering: true}
169 var lon = json_js.properties.lon;
170 var lat = json_js.properties.lat;
171 var zoom = json_js.properties.zoom;
172 var width = json_js.properties.width;
173 var height = json_js.properties.height;
174 if (zoom === undefined) zoom = 10; // default zoom
175 if (width === undefined) width = '100%'; // default width
176 if (height === undefined) height = 450; // default: 450 pixel
178 jq_map.height(height);
179 var map = new OpenLayers.Map(jq_map.context, {
180 projection: EPSG3857,
181 displayProjection: EPSG4326,
184 layers: [layer_map, layer_path, layer_point],
185 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
191 // if this would be done before the layer is added to the map, the features are not added
192 layer_path.addFeatures(features_path);
193 layer_point.addFeatures(features_point);
195 // disable mouse wheel zoom
196 var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
197 navigation_control.disableZoomWheel();
200 // map.addControl(new OpenLayers.Control.LayerSwitcher());
202 // print sledrun name when mouse moves over it
203 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
207 renderIntent: "highlight"
210 // show popup when user clicks on a sledrun icon
211 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
214 onSelect: function(feature) {
216 if (feature.attributes.type == 'sledrun') {
217 popup_text += "<h2>" + feature.attributes.name + '</h2>\n' +
219 '<li><a href="' + feature.attributes.wiki + '">Details zur Rodelbahn</a></li>\n' +
220 '<li>Rodelbahnzustand<br/>';
221 if (feature.attributes.condition !== undefined) {
222 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: 'Geht nicht'};
223 var year_month_day = feature.attributes.date_report.split('-');
224 popup_text += '<a href="' + feature.attributes.wiki + '#Eintr.C3.A4ge">' + condition_text[feature.attributes.condition] + '</a> ' +
225 '<small>' + year_month_day[2] + '.' + year_month_day[1] + '.</small> ' +
226 '<em><a href="' + feature.attributes.wiki + '#Eintragen">Neu</a></em>';
228 popup_text += '<em><a href="' + feature.attributes.wiki + '#Eintragen">Bitte eintragen</a></em>';
230 popup_text += '</li>\n</ul>\n';
231 } else if (feature.attributes.wiki !== undefined) {
232 if (feature.attributes.name != undefined) popup_text += '<h2>' + feature.attributes.name + '</h2>\n';
233 popup_text += '<p><a href="' + feature.attributes.wiki + '">Details</a></p>\n';
237 var selectFeatureControl = this;
238 var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + feature.attributes.wiki,
239 feature.geometry.getBounds().getCenterLonLat(),
242 null, true, function(event) {
244 selectFeatureControl.unselectAll();
246 feature.popup = popup;
249 onUnselect: function(feature) {
250 if (feature.popup === null) return;
253 map.removePopup(feature.popup);
254 feature.popup.destroy();
255 feature.popup = null;
261 function init_wrmaps() {
262 var jq_maps = $('.wrmap'); // all wrmap <div> elements
263 jq_maps.each(init_wrmap);
267 $(document).ready(init_wrmaps);