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

View file

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