From 9dd2e1123817b7896de3f1c64e81eb89cef0cafc Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Wed, 13 Mar 2019 12:31:13 +0000 Subject: [PATCH] Do not refresh vehicle pathinfo on every ttss_refresh seconds --- map.js | 110 ++++++++++++++++++++++++++++++++++++------------------- 1 files changed, 72 insertions(+), 38 deletions(-) diff --git a/map.js b/map.js index 4640626..8e78e93 100644 --- a/map.js +++ b/map.js @@ -58,6 +58,7 @@ }), }; var stops_type = ['st', 'sb', 'pt', 'pb']; +var stops_mapping = {}; var stops_source = {}; var stops_layer = {}; @@ -67,6 +68,7 @@ var feature_clicked = null; var feature_xhr = null; var feature_timer = null; +var path_xhr = null; var route_source = null; var route_layer = null; @@ -300,6 +302,7 @@ } else { vehicle_feature.setProperties(vehicle); vehicle_feature.getStyle().getImage().setRotation(Math.PI * parseFloat(vehicle.heading ? vehicle.heading : 0) / 180.0); + vehicle_feature.getStyle().getText().setText(vehicle.line); } } @@ -369,6 +372,7 @@ function updateStopSource(stops, prefix) { var source = stops_source[prefix]; + var mapping = stops_mapping[prefix]; for(var i = 0; i < stops.length; i++) { var stop = stops[i]; @@ -377,6 +381,12 @@ stop.geometry = getGeometry(stop); var stop_feature = new ol.Feature(stop); + + if(prefix.startsWith('p')) { + mapping[stop.stopPoint] = stop_feature; + } else { + mapping[stop.shortName] = stop_feature; + } stop_feature.setId(prefix + stop.id); @@ -408,20 +418,54 @@ }).fail(fail_ajax); } -function vehicleTable(tripId, table, featureId) { - if(feature_xhr) feature_xhr.abort(); - if(feature_timer) clearTimeout(feature_timer); +function vehiclePath(feature, tripId) { + if(path_xhr) path_xhr.abort(); + + var featureId = feature.getId(); + var vehicleId = featureId.substr(1); var url = ttss_trams_base; if(featureId.startsWith('b')) { url = ttss_buses_base; } - var vehicleId = featureId.substr(1); + path_xhr = $.get( + url + '/geoserviceDispatcher/services/pathinfo/vehicle' + + '?id=' + encodeURIComponent(vehicleId) + ).done(function(data) { + if(!data || !data.paths || !data.paths[0] || !data.paths[0].wayPoints) return; + + var point = null; + var points = []; + for(var i = 0; i < data.paths[0].wayPoints.length; i++) { + point = data.paths[0].wayPoints[i]; + points.push(ol.proj.fromLonLat([ + point.lon / 3600000.0, + point.lat / 3600000.0, + ])); + } + + route_source.addFeature(new ol.Feature({ + geometry: new ol.geom.LineString(points) + })); + route_layer.setVisible(true); + }); +} + +function vehicleTable(feature, table) { + if(feature_xhr) feature_xhr.abort(); + if(feature_timer) clearTimeout(feature_timer); + + var featureId = feature.getId(); + + var url = ttss_trams_base; + if(featureId.startsWith('b')) { + url = ttss_buses_base; + } feature_xhr = $.get( url + '/services/tripInfo/tripPassages' - + '?tripId=' + encodeURIComponent(tripId) + + '?tripId=' + encodeURIComponent(feature.get('tripId')) + '&mode=departure' ).done(function(data) { if(!data.routeName || !data.directionText) { @@ -456,31 +500,7 @@ markStops(stopsToMark, featureId.substr(0,1), true); - feature_timer = setTimeout(function() { vehicleTable(tripId, table, featureId); }, ttss_refresh); - - if(!vehicleId) return; - - feature_xhr = $.get( - url + '/geoserviceDispatcher/services/pathinfo/vehicle' - + '?id=' + encodeURIComponent(vehicleId) - ).done(function(data) { - if(!data || !data.paths || !data.paths[0] || !data.paths[0].wayPoints) return; - - var point = null; - var points = []; - for(var i = 0; i < data.paths[0].wayPoints.length; i++) { - point = data.paths[0].wayPoints[i]; - points.push(ol.proj.fromLonLat([ - point.lon / 3600000.0, - point.lat / 3600000.0, - ])); - } - - route_source.addFeature(new ol.Feature({ - geometry: new ol.geom.LineString(points) - })); - route_layer.setVisible(true); - }); + feature_timer = setTimeout(function() { vehicleTable(feature, table); }, ttss_refresh); }).fail(fail_ajax_popup); } @@ -593,14 +613,30 @@ addElementWithText(thead, 'th', lang.header_time); addElementWithText(thead, 'th', lang.header_stop); - vehicleTable(feature.get('tripId'), tbody, feature.getId()); + vehicleTable(feature, tbody); + vehiclePath(feature); styleVehicle(feature, true); break; case 's': type = lang.type_stop_tram; + var second_type = lang.departures_for_buses; + var mapping = stops_mapping['sb']; if(feature.getId().startsWith('sb')) { type = lang.type_stop_bus; + second_type = lang.departures_for_trams; + mapping = stops_mapping['st']; + } + + if(mapping[feature.get('shortName')]) { + additional = document.createElement('p'); + additional.className = 'small'; + addElementWithText(additional, 'a', second_type).addEventListener( + 'click', + function() { + featureClicked(mapping[feature.get('shortName')]); + } + ); } addElementWithText(thead, 'th', lang.header_line); @@ -622,12 +658,8 @@ addElementWithText(additional, 'a', lang.departures_for_stop).addEventListener( 'click', function() { - var source = stops_source['s' + feature.getId().substr(1,1)]; - featureClicked(source.forEachFeature(function(stop_feature) { - if(stop_feature.get('shortName') == feature.get('shortName')) { - return stop_feature; - } - })); + var mapping = stops_mapping['s' + feature.getId().substr(1,1)]; + featureClicked(mapping[feature.get('shortName')]); } ); @@ -671,6 +703,7 @@ unstyleSelectedFeatures(); feature_clicked = null; + if(path_xhr) path_xhr.abort(); if(feature_xhr) feature_xhr.abort(); if(feature_timer) clearTimeout(feature_timer); } @@ -851,7 +884,7 @@ if(!f1) return f2; if(!f2) return f1; - return (getDistance(point, f1) < getDistance(point, f2)) ? f1 : f2; + return (getDistance(point, f1) <= getDistance(point, f2)) ? f1 : f2; } function init() { @@ -876,6 +909,7 @@ renderMode: 'image', style: stops_style[type], }); + stops_mapping[type] = {}; }); stop_selected_source = new ol.source.Vector({ -- Gitblit v1.9.1