Commit 6fd985ae authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

show close stations via geolocation

parent d95cb9d0
Loading
Loading
Loading
Loading
+31 −1
Original line number Diff line number Diff line
@@ -577,7 +577,7 @@ helper 'navbar_class' => sub {

get '/' => sub {
	my ($self) = @_;
	$self->render('landingpage');
	$self->render( 'landingpage', with_geolocation => 1 );
};

post '/action' => sub {
@@ -640,6 +640,36 @@ post '/action' => sub {
	}
};

post '/x/geolocation' => sub {
	my ($self) = @_;

	my $lon = $self->param('lon');
	my $lat = $self->param('lat');

	if ( not $lon or not $lat ) {
		$self->render( json => { error => 'Invalid lon/lat received' } );
	}
	else {
		my @candidates = map {
			{
				ds100    => $_->[0][0],
				name     => $_->[0][1],
				eva      => $_->[0][2],
				lon      => $_->[0][3],
				lat      => $_->[0][4],
				distance => $_->[1],
			}
		} Travel::Status::DE::IRIS::Stations::get_station_by_location( $lon,
			$lat, 5 );
		$self->render(
			json => {
				candidates => [@candidates],
			}
		);
	}

};

get '/*station' => sub {
	my ($self) = @_;
	my $station = $self->stash('station');
+55 −0
Original line number Diff line number Diff line
$(document).ready(function() {
	var prePlaceholder = $('p.geolocationhint');
	var placeholder = $('div.geolocation div.progress');
	var showError = function(header, message, code) {
		prePlaceholder.remove();
		placeholder.remove();
	};

	var processResult = function(data) {
		if (data.error) {
			showError('Backend-Fehler:', data.error, null);
		} else if (data.candidates.length == 0) {
			showError('Keine Bahnhöfe in 70km Umkreis gefunden', '', null);
		} else {
			resultTable = $('<table><tbody></tbody></table>')
			resultBody = resultTable.children();
			$.each(data.candidates, function(i, candidate) {

				var ds100 = candidate.ds100,
					name = candidate.name,
					distance = candidate.distance;
				distance = distance.toFixed(1);

				var stationlink = $(document.createElement('a'));
				stationlink.attr('href', ds100);
				stationlink.text(name);

				resultBody.append('<tr><td><a href="/' + ds100 + '">' + name + '</a></td></tr>');
			});
			placeholder.replaceWith(resultTable);
		}
	};

	var processLocation = function(loc) {
		$.post('/x/geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, processResult);
	};

	var processError = function(error) {
		if (error.code == error.PERMISSION_DENIED) {
			showError('Standortanfrage nicht möglich.', 'Vermutlich fehlen die Rechte im Browser oder der Android Location Service ist deaktiviert.', 'geolocation.error.PERMISSION_DENIED');
		} else if (error.code == error.POSITION_UNAVAILABLE) {
			showError('Standort konnte nicht ermittelt werden', '(Service nicht verfügbar)', 'geolocation.error.POSITION_UNAVAILABLE');
		} else if (error.code == error.TIMEOUT) {
			showError('Standort konnte nicht ermittelt werden', '(Timeout)', 'geolocation.error.TIMEOUT');
		} else {
			showError('Standort konnte nicht ermittelt werden', '(unbekannter Fehler)', 'unknown geolocation.error code');
		}
	};

	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(processLocation, processError);
	} else {
		showError('Standortanfragen werden von diesem Browser nicht unterstützt', '', null);
	}
});
+4 −0
Original line number Diff line number Diff line
@@ -25,6 +25,10 @@
				<div class="card-content white-text">
					<span class="card-title">Hallo, <%= $self->get_user_name %>!</span>
					<p>Du bist gerade nicht eingecheckt.</p>
					<p class="geolocationhint">Stationen in der Umgebung:</p>
					<div class="geolocation">
						<div class="progress"><div class="indeterminate"></div></div>
					</div>
				</div>
			</div>
		% }
+3 −0
Original line number Diff line number Diff line
@@ -10,6 +10,9 @@
	%= javascript '/static/js/jquery-2.2.4.min.js'
	%= javascript '/static/js/materialize.min.js'
	%= javascript '/static/js/travelynx-actions.js'
	% if (stash('with_geolocation')) {
		%= javascript '/static/js/geolocation.js'
	% }
</head>
<body>