summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2016-05-10 12:21:32 +0200
committerDaniel Friesel <derf@finalrewind.org>2016-05-10 12:21:32 +0200
commit6c37dec8ed54a987acb6665fb6525a39fc6ca5da (patch)
tree290e831d119697ff4dd84468e256766c63fd57f2
parente09a676a9fac68feca73caffea792c4f0f8eef38 (diff)
geolocation playground
-rw-r--r--index.pl22
-rw-r--r--public/static/default.css12
-rw-r--r--public/static/geolocation.js30
-rw-r--r--templates/geolocation.html.ep2
-rw-r--r--templates/layouts/default.html.ep6
5 files changed, 72 insertions, 0 deletions
diff --git a/index.pl b/index.pl
index 543bcd1..155fd18 100644
--- a/index.pl
+++ b/index.pl
@@ -810,6 +810,28 @@ get '/_redirect' => sub {
}
};
+get '/_auto' => sub {
+ my $self = shift;
+
+ $self->render('geolocation', with_geolocation => 1, hide_opts => 1);
+};
+
+post '/_geolocation' => sub {
+ my $self = shift;
+
+ 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 {
+ $self->render(json => {
+ candidates => [ Travel::Status::DE::IRIS::Stations::get_stations_by_location($lon, $lat, 10) ],
+ });
+ }
+};
+
app->defaults( layout => 'default' );
get '/' => \&handle_request;
diff --git a/public/static/default.css b/public/static/default.css
index e18d705..222c9da 100644
--- a/public/static/default.css
+++ b/public/static/default.css
@@ -404,6 +404,18 @@ div.displaysingle div.info {
color: #0000ff;
}
+div.candidatelist a {
+ display: block;
+ text-decoration: none;
+ color: black;
+ padding: 1em;
+ margin-top: 0.1em;
+ margin-bottom: 0.1em;
+ width: 20em;
+ text-align: center;
+ background-color: #dddddd;
+}
+
div.about {
font-family: Sans-Serif;
color: #666666;
diff --git a/public/static/geolocation.js b/public/static/geolocation.js
new file mode 100644
index 0000000..78b0691
--- /dev/null
+++ b/public/static/geolocation.js
@@ -0,0 +1,30 @@
+$(document).ready(function() {
+ var processResult = function(data) {
+ if (data.error) {
+ $('div.candidatelist').text(data.error);
+ } else {
+ $.each(data.candidates, function(i, candidate) {
+
+ var ds100 = candidate[0][0],
+ name = candidate[0][1],
+ distance = candidate[1];
+ distance = distance.toFixed(1);
+
+ var stationlink = $(document.createElement('a'));
+ stationlink.attr('href', ds100);
+ stationlink.text(name);
+ $('div.candidatelist').append(stationlink);
+ });
+ }
+ };
+
+ var processLocation = function(loc) {
+ $.post('/_geolocation', {lon: loc.coords.longitude, lat: loc.coords.latitude}, processResult);
+ };
+
+ if (navigator.geolocation) {
+ navigator.geolocation.getCurrentPosition(processLocation);
+ } else {
+ $('div.candidatelist').text('Geolocation is not supported by your browser');
+ }
+});
diff --git a/templates/geolocation.html.ep b/templates/geolocation.html.ep
new file mode 100644
index 0000000..a0667db
--- /dev/null
+++ b/templates/geolocation.html.ep
@@ -0,0 +1,2 @@
+<div class="candidateheader">Nächstgelegene Stationen:</div>
+<div class="candidatelist"></div>
diff --git a/templates/layouts/default.html.ep b/templates/layouts/default.html.ep
index 9348852..f57ceab 100644
--- a/templates/layouts/default.html.ep
+++ b/templates/layouts/default.html.ep
@@ -20,6 +20,9 @@
%= javascript begin
$(function () { $('marquee').marquee() });
% end
+ % if (stash('with_geolocation')) {
+ %= javascript '/static/geolocation.js'
+ % }
</head>
<body style="<%= (stash('hide_opts') ? 'margin: 0; padding: 0;' : q{}) %>">
@@ -59,6 +62,9 @@ on 2015-01-02 at 07:00 CET.</div>
<p>
Alle Angaben ohne Gewähr.
</p>
+<p class="geolink">
+<a href="<%= url_for('_auto')->to_abs->scheme('https') %>">Nahe gelegene Bahnhöfe suchen</a>
+</p>
% }