3 // "User defined" popup class to be able to specify a minimum size for the popup,
4 // so that Safari 7.0 displays it correctly (see ticket #89).
5 OpenLayers.Popup.WrInfo = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
6 minSize: new OpenLayers.Size(180, 260),
8 initialize: function(id, lonlat, contentSize, contentHTML, anchor, closeBox, closeCallback) {
9 OpenLayers.Popup.FramedCloud.prototype.initialize.apply(this, arguments);
12 CLASS_NAME: "OpenLayers.Popup.WrInfo"
17 function init_wrmap(i, jq_map) {
19 var EPSG4326 = ol.proj.get("EPSG:4326"); // lon/lat
20 var EPSG3857 = ol.proj.get("EPSG:3857"); // google
24 function createElement(tagName, attributes) {
25 var element = $(document.createElement(tagName));
26 if (attributes === undefined) return element;
27 for (var attribute in attributes) {
28 element.attr(attribute, attributes[attribute]);
33 function appendElement(parentElement, tagName, attributes) {
34 if (attributes === undefined) attributes = {};
35 var element = createElement(tagName, attributes);
36 parentElement.append(element);
42 // extract geojson from map element and clear map element's content
44 var ext_path = jq_map.attr('data-ext-path'); // e.g. '/mediawiki/extensions/wrmap'
45 var img_path = ext_path + '/img';
46 //OpenLayers.ImgPath = ext_path + '/openlayers/img/'; // e.g. '/mediawiki/extensions/wrmap/openlayers/img/'
47 var json_string = jq_map.children().last().text();
48 jq_map.empty(); // once parsed, remove geojson string from the map element.
49 var json_js = JSON.parse(json_string);
50 var format_geojson = new ol.format.GeoJSON();
51 var features_all = format_geojson.readFeatures(json_js, {dataProjection: EPSG4326, featureProjection: EPSG3857});
53 // extract, tranform and split features to layers
55 var features_path = new Array();
56 var features_point = new Array();
57 for (var i = 0; i != features_all.length; ++i) {
58 var feature = features_all[i];
59 feature.geometry.transform(EPSG4326, EPSG3857);
60 if (feature.geometry instanceof OpenLayers.Geometry.Point) features_point.push(feature);
61 else features_path.push(feature);
69 var layer_map = new ol.layer.Tile({
70 source: new ol.source.OSM()
75 // Microsoft Bing Maps
76 // var layer_map = new OpenLayers.Layer.Bing({
78 // key: "AgPH3SlIXAwajrJKf0FORQyhTqsP8KIlvtN6RKfvxe6fOB6q6-HFmg8EOFm7LSOA",
79 // tileOptions: {crossOriginKeyword: null}});
81 // // Alternative: Base map
82 // // see: http://www.basemap.at
84 // // Alternative: OpenTopoMap
85 // // see: https://opentopomap.org/about
87 // // Alternative: Dummy base layer
88 // var layer_map = new OpenLayers.Layer.Vector("Base Layer", {
89 // isBaseLayer: true});
96 function get_feature_title(feature) {
97 var title = feature.get('type');
98 title = title.charAt(0).toUpperCase() + title.slice(1); // first letter uppercase
99 if (feature.get('name')) title += ': ' + feature.get('name');
103 function style_point_function(feature, resolution) {
104 var src = img_path + '/marker_p_' + feature.get('type') + '.png';
105 return new ol.style.Style({
106 image: new ol.style.Icon({
114 function style_point_function_selected(feature, resolution) {
115 var style = style_point_function(feature, resolution);
116 style.setText(new ol.style.Text({
117 text: get_feature_title(feature),
120 stroke: new ol.style.Stroke({
128 function style_path_function(feature, resolution) {
130 'rodelbahn': '#014e9a',
132 'alternative': '#7f7fff',
136 var color = feature.get('strokeColor') || line_color[feature.get('type')] || '#e7525b';
137 var width = (feature.get('type') in ['lift', 'anfahrt']) ? 3 : 6;
138 return new ol.style.Style({
139 stroke: new ol.style.Stroke({
146 function style_function(feature, resolution) {
147 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function(feature, resolution);
148 return style_path_function(feature, resolution);
151 function style_function_selected(feature, resolution) {
152 if (feature.getGeometry() instanceof ol.geom.Point) return style_point_function_selected(feature, resolution);
153 return style_path_function(feature, resolution);
158 var layer_path = new OpenLayers.Layer.Vector("Path", {
159 styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
160 strokeColor: '${getStrokeColor}',
161 strokeWidth: '${getStrokeWidth}'
164 getStrokeColor: function(feature) {
165 if (feature.attributes.strokeColor !== undefined) return feature.attributes.strokeColor;
166 if (feature.attributes.type == 'rodelbahn') return '#014e9a';
167 if (feature.attributes.type == 'gehweg') return '#e98401';
168 if (feature.attributes.type == 'alternative') return '#7f7fff';
169 if (feature.attributes.type == 'lift') return '#000000';
170 if (feature.attributes.type == 'anfahrt') return '#e1e100';
173 getStrokeWidth: function(feature) {
174 if (feature.attributes.strokeWidth !== undefined) return feature.attributes.strokeWidth;
175 if (feature.attributes.type == 'lift' || feature.attributes.type == 'anfahrt') return 3;
185 var filter_point_sledrun = new OpenLayers.Filter.Comparison({
186 type: OpenLayers.Filter.Comparison.EQUAL_TO,
191 // Returns 0 to 5 for features that represent sledruns as their condition
192 var get_sledrun_condition = function(feature) {
193 if (feature.attributes.condition === undefined) return 0;
194 return feature.attributes.condition;
198 var layer_point = new OpenLayers.Layer.Vector("Point", {
199 styleMap: new OpenLayers.StyleMap({
200 'default': new OpenLayers.Style({
202 backgroundGraphicZIndex: 11,
205 // the following context functions should only be available in the rule that uses them,
206 // but the rule dependent contexts are ignored by OpenLayers (I think that's a bug)
207 getCondition: get_sledrun_condition,
208 getSymbol: function(feature) {
209 var name = feature.attributes.type;
214 new OpenLayers.Rule({
215 filter: filter_point_sledrun,
217 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nn.png',
222 backgroundGraphic: img_path + '/marker_c_shadow.png',
224 backgroundHeight: 23,
225 backgroundXOffset: -8,
226 backgroundYOffset: -8,
229 new OpenLayers.Rule({
232 externalGraphic: img_path + '/marker_p_${getSymbol}.png',
241 highlight: new OpenLayers.Style({
242 label: "${getTitle}",
243 labelOutlineColor: "white",
247 getCondition: get_sledrun_condition,
248 getTitle: function(feature) {
250 if (feature.attributes.type != 'point') {
251 title = feature.attributes.type;
252 title = title.charAt(0).toUpperCase() + title.slice(1); // First letter uppercase
253 if (feature.attributes.name !== undefined) title += ': ';
255 if (feature.attributes.name !== undefined) title += feature.attributes.name;
260 new OpenLayers.Rule({
261 filter: filter_point_sledrun,
265 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png'
268 new OpenLayers.Rule({
276 select: new OpenLayers.Style({
280 getCondition: get_sledrun_condition,
283 new OpenLayers.Rule({
284 filter: filter_point_sledrun,
286 externalGraphic: img_path + '/marker_c_sledrun_${getCondition}nh.png',
287 backgroundGraphic: false,
292 new OpenLayers.Rule({
298 rendererOptions: {yOrdering: true}
304 var lon = json_js.properties.lon;
305 var lat = json_js.properties.lat;
306 var zoom = json_js.properties.zoom;
307 var width = json_js.properties.width;
308 var height = json_js.properties.height;
309 if (zoom === undefined) zoom = 10; // default zoom
310 if (width === undefined) width = '100%'; // default width
311 if (height === undefined) height = 450; // default: 450 pixel
313 jq_map.height(height);
315 var map = new OpenLayers.Map(jq_map.context, {
316 projection: EPSG3857,
317 displayProjection: EPSG4326,
320 layers: [layer_map, layer_path, layer_point],
321 center: new OpenLayers.LonLat(lon, lat).transform(EPSG4326, EPSG3857),
326 var layer_sledrun_source = new ol.source.Vector({features: features_all});
327 var layer_sledrun = new ol.layer.Vector({
328 source: layer_sledrun_source,
329 style: style_function
332 var map = new ol.Map({
333 target: jq_map.context,
339 center: ol.proj.fromLonLat([lon, lat]),
342 controls: ol.control.defaults({
343 attributionOptions: {
350 var select_hover = new ol.interaction.Select({
351 condition: ol.events.condition.pointerMove,
352 style: style_function_selected,
354 map.addInteraction(select_hover);
355 select_hover.on('select', function(event) {
361 // if this would be done before the layer is added to the map, the features are not added
362 layer_path.addFeatures(features_path);
363 layer_point.addFeatures(features_point);
365 // disable mouse wheel zoom
366 var navigation_control = map.getControlsByClass('OpenLayers.Control.Navigation')[0];
367 navigation_control.disableZoomWheel();
370 // map.addControl(new OpenLayers.Control.LayerSwitcher());
372 // print sledrun name when mouse moves over it
373 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
377 renderIntent: "highlight"
380 // show popup when user clicks on a sledrun icon
381 map.addControl(new OpenLayers.Control.SelectFeature(layer_point, {
384 onSelect: function(feature) {
385 var attr = feature.attributes;
386 var popup_div = createElement('div');
389 if (attr.name !== undefined && (attr.wiki !== undefined || attr.thumb_url !== undefined)) {
390 var h2 = appendElement(popup_div, 'h2');
391 if (attr.wiki === undefined) h2.text(attr.name);
392 else appendElement(h2, 'a', {href: attr.wiki}).text(attr.name);
395 // sledrun information
396 if (attr.type == 'sledrun') {
397 var p = appendElement(popup_div, 'p').text('Rodelbahnzustand').append(createElement('br'));
398 if (attr.condition !== undefined) {
399 var condition_text = {1: 'Sehr gut', 2: 'Gut', 3: 'Mittelmäßig', 4: 'Schlecht', 5: 'Geht nicht'};
400 var year_month_day = attr.date_report.split('-');
401 p.append(createElement('a', {href: attr.wiki + '#Eintr.C3.A4ge'}).text(condition_text[attr.condition]), ' ');
402 p.append(createElement('small').text(year_month_day[2] + '.' + year_month_day[1] + '.'), ' ');
403 p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Neu')));
405 p.append(createElement('em').append(createElement('a', {href: attr.wiki + '#Eintragen'}).text('Bitte eintragen')));
410 if (attr.wiki !== undefined) {
411 var a = appendElement(appendElement(popup_div, 'p'), 'a', {href: attr.wiki});
412 var detail_text = 'Details';
413 if (attr.type == 'sledrun') detail_text += ' zur Rodelbahn';
414 if (attr.type == 'gasthaus') detail_text += ' zum Gasthaus';
415 if (attr.thumb_url === undefined) a.text(detail_text);
416 else a.append(createElement('img', {src: attr.thumb_url, alt: detail_text, title: detail_text}));
419 // no popup if we don't have anything to say
420 if (popup_div.children().length == 0) return;
423 var selectFeatureControl = this;
424 var popup = new OpenLayers.Popup.WrInfo('sledruninfopopup_' + attr.wiki,
425 feature.geometry.getBounds().getCenterLonLat(),
428 null, true, function(event) {
430 selectFeatureControl.unselectAll();
432 feature.popup = popup;
435 onUnselect: function(feature) {
436 if (feature.popup === null) return;
439 map.removePopup(feature.popup);
440 feature.popup.destroy();
441 feature.popup = null;
447 function init_wrmaps() {
448 var jq_maps = $('.wrmap'); // all wrmap <div> elements
449 jq_maps.each(init_wrmap);
453 $(document).ready(init_wrmaps);