summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-11-26 19:44:47 +0100
committerDaniel Friesel <derf@finalrewind.org>2019-11-26 19:44:47 +0100
commit81b1a0d1c56a667494ecd82b472f92207a4b68f7 (patch)
treeda6d8e3056a8140ca692cf58365f16f1216eaf3c
parent5e03718f14e9d8888a3bd0407828abf01beeda96 (diff)
Show train route via map on public status pagefeature/status-map
-rwxr-xr-xlib/Travelynx/Controller/Traveling.pm65
-rw-r--r--templates/_map.html.ep17
-rw-r--r--templates/user_status.html.ep2
3 files changed, 79 insertions, 5 deletions
diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm
index 56de0fb..8052485 100755
--- a/lib/Travelynx/Controller/Traveling.pm
+++ b/lib/Travelynx/Controller/Traveling.pm
@@ -35,6 +35,8 @@ sub user_status {
my $ts = $self->stash('ts');
my $user = $self->get_privacy_by_name($name);
+ my $location = $self->app->coordinates_by_station;
+
if ( $user and ( $user->{public_level} & 0x02 ) ) {
my $status = $self->get_user_status( $user->{id} );
@@ -45,6 +47,11 @@ sub user_status {
->to_abs->scheme('https'),
);
+ my @station_coordinates;
+ my @station_pairs;
+ my @train_coordinates;
+ my @train_line;
+
if (
$ts
and ( not $status->{checked_in}
@@ -67,17 +74,63 @@ sub user_status {
$tw_data{description} .= $status->{real_arrival}
->strftime(' – Ankunft gegen %H:%M Uhr');
}
+ if ( exists $location->{ $status->{dep_name} } ) {
+ push( @station_coordinates,
+ [ $location->{ $status->{dep_name} }, $status->{dep_name} ]
+ );
+ if ( $status->{departure_countdown} > 0 ) {
+ @train_coordinates = $location->{ $status->{dep_name} };
+ }
+ }
+ my $prev_station = [ $status->{dep_name} ];
+ for my $station ( @{ $status->{route_after} } ) {
+ my $prev_name = $prev_station->[0];
+ my $station_name = $station->[0];
+ if ( exists $location->{$prev_name}
+ and exists $location->{$station_name} )
+ {
+ push( @station_pairs,
+ [ $location->{$prev_name}, $location->{$station_name} ]
+ );
+ push( @station_coordinates,
+ [ $location->{$station_name}, $station_name ] );
+ if ( not @train_coordinates and not @train_line ) {
+ if ( ( $station->[1]{rt_arr_countdown} // 0 ) > 0 ) {
+ @train_line = [
+ $location->{$prev_name},
+ $location->{$station_name}
+ ];
+ }
+ elsif ( ( $station->[1]{rt_dep_countdown} // 0 ) > 0 ) {
+ @train_coordinates = $location->{$station_name};
+ }
+ }
+ }
+ $prev_station = $station;
+ }
}
else {
$tw_data{title} = "${name} ist gerade nicht eingecheckt";
$tw_data{description} = "Letztes Fahrtziel: $status->{arr_name}";
+ if ( $status->{arr_name}
+ and exists $location->{ $status->{arr_name} } )
+ {
+ push( @station_coordinates,
+ [ $location->{ $status->{arr_name} }, $status->{arr_name} ]
+ );
+ }
}
$self->render(
'user_status',
- name => $name,
- journey => $status,
- twitter => \%tw_data,
+ name => $name,
+ with_map => 1,
+ journey => $status,
+ twitter => \%tw_data,
+ station_coordinates => \@station_coordinates,
+ station_pairs => \@station_pairs,
+ marker_coordinates => \@train_coordinates,
+ marker_lines => \@train_line,
);
}
else {
@@ -97,8 +150,10 @@ sub public_status_card {
my $status = $self->get_user_status( $user->{id} );
$self->render(
'_public_status_card',
- name => $name,
- journey => $status
+ name => $name,
+ journey => $status,
+ station_coordinates => [],
+ station_pairs => [],
);
}
else {
diff --git a/templates/_map.html.ep b/templates/_map.html.ep
index cceec01..31223ed 100644
--- a/templates/_map.html.ep
+++ b/templates/_map.html.ep
@@ -19,6 +19,12 @@ L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
+var markers = [
+% for my $marker ( @{stash('marker_coordinates') // [] } ) {
+[<%= $marker->[0] %>,<%= $marker->[1] %>],
+% }
+];
+
var stations = [
% for my $station ( @{stash('station_coordinates') // [] } ) {
[[<%= $station->[0][0] %>,<%= $station->[0][1] %>],'<%= $station->[1] %>'],
@@ -31,6 +37,12 @@ var routes = [
% }
];
+var blue_routes = [
+% for my $pair ( @{stash('blue_routes') // [] } ) {
+[[<%= $pair->[0][0] %>,<%= $pair->[0][1] %>],[<%= $pair->[1][0] %>,<%= $pair->[1][1] %>]],
+% }
+];
+
for (var station_id in stations) {
L.circle(stations[station_id][0], {
color: '#f03',
@@ -40,7 +52,12 @@ for (var station_id in stations) {
}).bindPopup(stations[station_id][1]).addTo(map);
}
+for (var marker_id in markers) {
+ L.marker(markers[marker_id]).addTo(map);
+}
+
var pl = L.polyline(routes, {color: '#f09', opacity: 0.5}).addTo(map);
+L.polyline(blue_routes, {color: '#09f', opacity: 1}).addTo(map);
if (routes.length) {
map.fitBounds(pl.getBounds());
}
diff --git a/templates/user_status.html.ep b/templates/user_status.html.ep
index 2a6be03..bc88037 100644
--- a/templates/user_status.html.ep
+++ b/templates/user_status.html.ep
@@ -3,3 +3,5 @@
%= include '_public_status_card', name => $name, journey => $journey
</div>
</div>
+
+%= include '_map', markers => $marker_coordinates, station_coordinates => $station_coordinates, station_pairs => $station_pairs, blue_routes => $marker_lines