var apiUrl = "http://api.trafikinfo.trafikverket.se/beta/";
$.support.cors = true; // Enable Cross domain requests
//Initialize the map $(document).ready(function () { try { $.ajaxSetup({ url: apiUrl + "data.json", error: function (msg) { if (msg.statusText == "abort") return; alert("Request failed: " + msg.statusText + "\n" + msg.responseText); } }); } catch (e) { alert("Ett fel uppstod vid initialisering."); }
//Creates the OpenLayers map object (http://openlayers.org/en/master/apidoc/ol.Map.html) var map = new ol.Map({ controls: ol.control.defaults().extend([new ol.control.FullScreen()]), target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.MapQuest({ layer: 'osm' }) //Background layer, using tile layer from MapQuest }) ], view: new ol.View({ center: ol.proj.transform([15, 60], 'EPSG:4326', 'EPSG:3857'), //Transforms coordinates to the coordinate system used by the background layer zoom: 4 }) }); addVectorLayer(map, "Situations"); var element = document.getElementById('popup');
var popup = new ol.Overlay({ element: element, positioning: 'bottom-center', stopEvent: false }); map.addOverlay(popup);
$(window).resize(function () { $("#map").height($(window).height()); map.updateSize(); })
$(element).popover({ 'placement': 'top', 'html': true, template: '
', }); map.on('click', function (evt) { var node = evt.originalEvent.target.parentNode; while (node != null) { //Check if click is inside of popup, then ignore click on map if ($(node).hasClass("woopwoop") == true) return false; node = node.parentNode; } var features = []; map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) { features.push(feature); }); if (features.length == 0) { $(element).popover('hide'); } else { var geometry = features[0].getGeometry(); var coord = geometry.getCoordinates(); popup.setPosition(coord); var header = ""; var text = ""; if (features.length > 1) { //Multiple situations in search result, displaying first two header = "Sökresultat"; $.each(features, function (index, feature) { if (index > 1) { text += "
Sökningen gav " + features.length + " resultat. För att se mer information, zooma in i kartan och klicka på önskad företeelse."; return false; } var managedCause = feature.get('managedCause'); text += "
" + (deviation.MessageCode || deviation.RoadNumber || "") + ((deviation.Header || null) != null ? (" - " + deviation.Header) : "") + "
"; text += formatDeviationText(deviation); }); text += "
"
});
} else { //Single situation in search result header = "Sökresultat"; var managedCause = features[0].get('managedCause'); text += "
" + managedCause.MessageType + "
" + (deviation.MessageCode || deviation.RoadNumber || "") + ((deviation.Header || null) != null ? (" - " + deviation.Header) : "") + "
"; text += formatDeviationText(deviation); }); text += "
" } var offset = $(element).offset(); var top = offset.top; var height = $(document).outerHeight(); var vert = 0.5 * height - top; var vertPlacement = vert > 0 ? 'bottom' : 'top';
$(element).data('bs.popover').options.placement = vertPlacement; $(element).data('bs.popover').options.content = '
' + header + '
' + text
$(element).popover('show'); } });
$(window).trigger("resize"); });
//Function for adding vector layers to map function addVectorLayer(mapObject, type) {
var features = [];
var format = new ol.format.WKT();
switch (type) {
case "Situation":
default:
var xmlRequest = "
$.each(response.RESPONSE.RESULT[0].Situation, function (index, item) { var iconId = null; var managedCause = null; $.each(item.Deviation, function (index2, deviation) { if (deviation.MessageCode == 'Vägen avstängd') iconId = deviation.IconId; if (deviation.ManagedCause) managedCause = deviation; }); var active = isActive(managedCause); if (!active) { //If managed cause is not active, check if underlying is active and in that case, use active Icon $.each(item.Deviation, function (index2, deviation) { if (isActive(deviation)) { active = true; return false; //Check done, break loop } }); } if (typeof ((((managedCause || item.Deviation[0]) || []).Geometry || []).WGS84) != "undefined") { var feature = new ol.Feature({ geometry: format.readGeometry((managedCause || item.Deviation[0]).Geometry.WGS84).transform("EPSG:4326", "EPSG:3857"), data: item, iconId: iconId || managedCause.IconId, active: active, managedCause: managedCause }); features.push(feature); } }); var source = new ol.source.Vector({ //Creates a source for the vector layer features: features }); var getIcon = function (feature, resolution) { var active = feature.get("active"); if(resolution > 40) return apiUrl + "icons/" + feature.get("iconId") + (!active ? "Planned" : "") + "?type=png16x16"; else return apiUrl + "icons/" + feature.get("iconId") + (!active ? "Planned" : "") + "?type=png32x32"; }
var styleFunction = function (feature, resolution) { //Function to determine style of icons return [new ol.style.Style({ image: new ol.style.Icon(({ anchor: [0.5, 8], anchorXUnits: 'fraction', anchorYUnits: 'pixels', opacity: 0.75, src: getIcon(feature, resolution) })) })]; };
var vectorLayer = new ol.layer.Vector({ //Creates a layer for deviations source: source, style: styleFunction }); mapObject.addLayer(vectorLayer); } catch (ex) { } }, error: function (xhr, status, error) { var err = status; }, complete: function (xhr, status) { var status = status; } }); break; }
}
function formatDeviationText(deviation) { var text = ""; switch (deviation.MessageCode) { case "Körfältsavstängningar": text += (deviation.NumberOfLanesRestricted || null) != null ? (deviation.NumberOfLanesRestricted + " körfält avstäng" + (deviation.NumberOfLanesRestricted == 1 ? "t" : "da")) : ""; break; default: text += "
" + (((deviation.Message || "") + (deviation.TemporaryLimit || "")) || deviation.LocationDescriptor || "") + "
"; text += "
"; text += "
"; break; } return text; }
function formatDeviationTableRow(header, value) { if (typeof (value) == "undefined") return ""; return "
"; }
//Checks if a deviation is currently active
function isActive(deviation) {
var active = false;
if (typeof (deviation.Schedule) != "undefined") {
$.each(deviation.Schedule, function (scheduleIndex, schedule) {
var currentTime = new Date();
var startOfPeriod = new Date(schedule.StartOfPeriod);
var endOfPeriod = new Date(schedule.EndOfPeriod);
if (startOfPeriod <= currentTime && currentTime <= endOfPeriod)
active = true;
});
} else {
active = true;
}
if (typeof (deviation.StartTime) != "undefined") {
var startTime = new Date(deviation.StartTime);
var endTime = new Date(deviation.EndTime)
var currentTime = new Date();
if (startTime > currentTime && currentTime < endTime)
active = false;
}
deviation.active = active;
return active;
}