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 if (attributes === undefined) return element;
12 for (var attribute in attributes) {
13 element.attr(attribute, attributes[attribute]);
18 function appendElement(parentElement, tagName, attributes) {
19 if (attributes === undefined) attributes = {};
20 var element = createElement(tagName, attributes);
21 parentElement.append(element);
26 // extract geojson from map element and clear map element's content
28 var ext_path = jq_map.attr('data-ext-path'); // e.g. '/mediawiki/extensions/wrmap'
29 var img_path = ext_path + '/img';
30 OpenLayers.ImgPath = ext_path + '/openlayers/img/'; // e.g. '/mediawiki/extensions/wrmap/openlayers/img/'
31 var json_string = jq_map.children().last().text();
32 jq_map.empty(); // once parsed, remove geojson string from the map element.
33 var format_json = new OpenLayers.Format.JSON();
34 var json_js = format_json.read(json_string);
35 var format_geojson = new OpenLayers.Format.GeoJSON();
36 var features_all = format_geojson.read(json_js);
38 // extract, tranform and split features to layers
39 var features_path = new Array();
40 var features_point = new Array();
41 for (var i = 0; i != features_all.length; ++i) {
42 var feature = features_all[i];
43 feature.geometry.transform(EPSG4326, EPSG3857);
44 if (feature.geometry instanceof OpenLayers.Geometry.Point) features_point.push(feature);
45 else features_path.push(feature);
51 var layer_map = new OpenLayers.Layer.Google("Google Physical", {
52 type: google.maps.MapTypeId.TERRAIN
55 // // Alternative: OSM map
56 // var layer_map = new OpenLayers.Layer.OSM();
58 // // Alternative: Microsoft Bing Maps
59 // var layer_map = new OpenLayers.Layer.Bing({
61 // key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA"});
63 // // Alternative: Dummy base layer
64 // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
65 // isBaseLayer: true});
70 var layer_path = new OpenLayers.Layer.Vector("Path", {
71 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
72 strokeColor: '${getStrokeColor}',
73 strokeWidth: '${getStrokeWidth}'
76 getStrokeColor: function(feature) {
77 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
78 if (feature.attributes.type == 'rodelbahn') return '#014e9a';
79 if (feature.attributes.type == 'gehweg') return '#e98401';
80 if (feature.attributes.type == 'alternative') return '#7f7fff';
81 if (feature.attributes.type == 'lift') return '#000000';
82 if (feature.attributes.type == 'anfahrt') return '#e1e100';
85 getStrokeWidth: function(feature) {
86 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
87 if (feature.attributes.type == 'lift' || feature.attributes.type == 'anfahrt') return 3;
97 var filter_point_sledrun = new OpenLayers.Filter.Comparison({
98 type: OpenLayers.Filter.Comparison.EQUAL_TO,
103 // Returns 0 to 5 for features that represent sledruns as their condition
104 var get_sledrun_condition = function(feature) {
105 if (feature.attributes.condition === undefined) return 0;
106 return feature.attributes.condition;
110 var layer_point = new OpenLayers.Layer.Vector("Point", {
111 styleMap: new OpenLayers.StyleMap({
112 'default': new OpenLayers.Style({
114 backgroundGraphicZIndex: 11,
117 // the following context functions should only be available in the rule that uses them,
118 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
119 getCondition: get_sledrun_condition,
120 getSymbol: function(feature) {
121 var name = feature.attributes.type;
126 new OpenLayers.Rule({
127 filter: filter_point_sledrun,
129 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nn.png',
134 backgroundGraphic: img_path + '/marker_c_shadow.png',
136 backgroundHeight: 23,
137 backgroundXOffset: -8,
138 backgroundYOffset: -8,
141 new OpenLayers.Rule({
144 externalGraphic: img_path + '/marker_p_${getSymbol}.png',
153 highlight: new OpenLayers.Style({
154 label: "${getTitle}",
155 labelOutlineColor: "white",
159 getCondition: get_sledrun_condition,
160 getTitle: function(feature) {
162 if (feature.attributes.type != 'point') {
163 title = feature.attributes.type;
164 title = title.charAt(0).toUpperCase() + title.slice(1); // First letter uppercase
165 if (feature.attributes.name !== undefined) title += ': ';
167 if (feature.attributes.name !== undefined) title += feature.attributes.name;
172 new OpenLayers.Rule({
173 filter: filter_point_sledrun,
177 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png'
180 new OpenLayers.Rule({
188 select: new OpenLayers.Style({
192 getCondition: get_sledrun_condition,
195 new OpenLayers.Rule({
196 filter: filter_point_sledrun,
198 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png',
199 backgroundGraphic: false,
204 new OpenLayers.Rule({
210 rendererOptions: {yOrdering: true}
216 var lon = json_js.properties.lon;
217 var lat = json_js.properties.lat;
218 var zoom = json_js.properties.zoom;
219 var width = json_js.properties.width;
220 var height = json_js.properties.height;
221 if (zoom === undefined) zoom = 10; // default zoom
222 if (width === undefined) width = '100%'; // default width
223 if (height === undefined) height = 450; // default: 450 pixel
225 jq_map.height(height);
226 var map = new OpenLayers.Map(jq_map.context, {
227 projection: EPSG3857,
228 displayProjection: EPSG4326,
231 layers: [layer_map, layer_path, layer_point],
232 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
238 // if this would be done before the layer is added to the map, the features are not added
239 layer_path.addFeatures(features_path);
240 layer_point.addFeatures(features_point);
242 // disable mouse wheel zoom
243 var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
244 navigation_control.disableZoomWheel();
247 // map.addControl(new OpenLayers.Control.LayerSwitcher());
249 // print sledrun name when mouse moves over it
250 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
254 renderIntent: "highlight"
257 // show popup when user clicks on a sledrun icon
258 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
261 onSelect: function(feature) {
262 var attr = feature.attributes;
263 var popup_div = createElement('div');
266 if (attr.name !== undefined && (attr.wiki !== undefined || attr.thumb_url !== undefined)) {
267 var h2 = appendElement(popup_div, 'h2');
268 if (attr.wiki === undefined) h2.text(attr.name);
269 else appendElement(h2, 'a', {href: attr.wiki}).text(attr.name);
272 // sledrun information
273 if (attr.type == 'sledrun') {
274 var p = appendElement(popup_div, 'p').text('Rodelbahnzustand').append(createElement('br'));
275 if (attr.condition !== undefined) {
276 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: 'Geht nicht'};
277 var year_month_day = attr.date_report.split('-');
278 p.append(createElement('a', {href: attr.wiki + '#Eintr.C3.A4ge'}).text(condition_text[attr.condition]), ' ');
279 p.append(createElement('small').text(year_month_day[2] + '.' + year_month_day[1] + '.'), ' ');
280 p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Neu')));
282 p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Bitte eintragen')));
287 if (attr.wiki !== undefined) {
288 var a = appendElement(appendElement(popup_div, 'p'), 'a', {href: attr.wiki});
289 var detail_text = 'Details';
290 if (attr.type == 'sledrun') detail_text += ' zur Rodelbahn';
291 if (attr.type == 'gasthaus') detail_text += ' zum Gasthaus';
292 if (attr.thumb_url === undefined) a.text(detail_text);
293 else a.append(createElement('img', {src: attr.thumb_url, alt: detail_text, title: detail_text}));
296 // no popup if we don't have anything to say
297 if (popup_div.children().length == 0) return;
300 var selectFeatureControl = this;
301 var popup = new OpenLayers.Popup.FramedCloud('sledruninfopopup_' + attr.wiki,
302 feature.geometry.getBounds().getCenterLonLat(),
305 null, true, function(event) {
307 selectFeatureControl.unselectAll();
309 feature.popup = popup;
312 onUnselect: function(feature) {
313 if (feature.popup === null) return;
316 map.removePopup(feature.popup);
317 feature.popup.destroy();
318 feature.popup = null;
324 function init_wrmaps() {
325 var jq_maps = $('.wrmap'); // all wrmap <div> elements
326 jq_maps.each(init_wrmap);
330 $(document).ready(init_wrmaps);