Improve map performance by passing actual polylines for train routes

This commit is contained in:
Daniel Friesel 2020-02-07 23:19:01 +01:00
parent c2e0f377be
commit 3ddd1a1cf4
2 changed files with 10 additions and 17 deletions

View file

@ -3,6 +3,7 @@ use Mojo::Base 'Mojolicious::Controller';
use DateTime; use DateTime;
use DateTime::Format::Strptime; use DateTime::Format::Strptime;
use JSON;
use List::Util qw(uniq min max); use List::Util qw(uniq min max);
use List::UtilsBy qw(uniq_by); use List::UtilsBy qw(uniq_by);
use List::MoreUtils qw(first_index); use List::MoreUtils qw(first_index);
@ -459,6 +460,8 @@ sub map_history {
return; return;
} }
my $json = JSON->new->utf8;
my $include_manual = $self->param('include_manual') ? 1 : 0; my $include_manual = $self->param('include_manual') ? 1 : 0;
my $first_departure = $journeys[-1]->{rt_departure}; my $first_departure = $journeys[-1]->{rt_departure};
@ -471,7 +474,7 @@ sub map_history {
grep { exists $location->{$_} } @stations; grep { exists $location->{$_} } @stations;
my @station_pairs; my @station_pairs;
my @coord_pairs; my @polylines;
my %seen; my %seen;
my @skipped_journeys; my @skipped_journeys;
@ -516,17 +519,11 @@ sub map_history {
$seen{$key} = 1; $seen{$key} = 1;
@polyline = @polyline[ $from_index .. $to_index ]; @polyline = @polyline[ $from_index .. $to_index ];
my $prev_coord = shift @polyline; my @polyline_coords;
for my $coord (@polyline) { for my $coord (@polyline) {
push( push( @polyline_coords, [ $coord->[1], $coord->[0] ] );
@coord_pairs,
[
[ $prev_coord->[1], $prev_coord->[0] ],
[ $coord->[1], $coord->[0] ]
]
);
$prev_coord = $coord;
} }
push( @polylines, [@polyline_coords] );
} }
for my $journey (@beeline_journeys) { for my $journey (@beeline_journeys) {
@ -620,12 +617,12 @@ sub map_history {
station_coordinates => \@station_coordinates, station_coordinates => \@station_coordinates,
polyline_groups => [ polyline_groups => [
{ {
polylines => \@station_pairs, polylines => $json->encode( \@station_pairs ),
color => '#673ab7', color => '#673ab7',
opacity => $with_polyline ? 0.4 : 0.6, opacity => $with_polyline ? 0.4 : 0.6,
}, },
{ {
polylines => \@coord_pairs, polylines => $json->encode( \@polylines ),
color => '#673ab7', color => '#673ab7',
opacity => 0.8, opacity => 0.8,
} }

View file

@ -28,11 +28,7 @@ var stations = [
var routes = []; var routes = [];
var pl; var pl;
% for my $line_group ( @{ stash('polyline_groups') // [] } ) { % for my $line_group ( @{ stash('polyline_groups') // [] } ) {
routes = [ routes = [ <%= $line_group->{polylines} %> ];
% for my $pair ( @{$line_group->{polylines} // []} ) {
[[<%= $pair->[0][0] %>,<%= $pair->[0][1] %>],[<%= $pair->[1][0] %>,<%= $pair->[1][1] %>]],
% }
];
pl = L.polyline(routes, {color: '<%= $line_group->{color} %>', opacity: <%= $line_group->{opacity} %>}).addTo(map); pl = L.polyline(routes, {color: '<%= $line_group->{color} %>', opacity: <%= $line_group->{opacity} %>}).addTo(map);
% if ($line_group->{fit_bounds}) { % if ($line_group->{fit_bounds}) {
if (routes.length) { if (routes.length) {