Skip to content

Commit

Permalink
Merge pull request james#1 from lazyatom/master
Browse files Browse the repository at this point in the history
Find bus stops near me using geolocation
  • Loading branch information
james committed Sep 28, 2011
2 parents e1da316 + 684af6a commit b14b87d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
13 changes: 13 additions & 0 deletions bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
get '/' do
if params[:search] && params[:search] != ""
@search_results = STOPS.select {|x| x["name"] =~ /#{params[:search]}/i}
elsif params[:lat] && params[:lon] && params[:lat] != "" && params[:lon] != ""
@search_results = STOPS.select { |x| approximate_distance_between(x, params) < 0.005 }
@search_results.sort_by! { |x| approximate_distance_between(x, params) }
end
erb :index
end

get '/nearby' do
erb :nearby
end

get '/stop/:stop_id' do |stop_id|
@stop = STOPS.select {|x| x["id"] =~ /#{stop_id}/i}.first
get_stop_json(stop_id)
Expand Down Expand Up @@ -44,3 +51,9 @@ def get_stop_json(stop_id)
def make_request(stop_id)
Net::HTTP.get(URI.parse("http://countdown.tfl.gov.uk/stopBoard/#{stop_id}/"))
end

def approximate_distance_between(stop, coords)
lat_diff = (stop["lat"].to_f - coords[:lat].to_f).abs
lon_diff = (stop["lng"].to_f - coords[:lon].to_f).abs
diff = Math.sqrt(lat_diff**2 + lon_diff**2)
end
5 changes: 3 additions & 2 deletions templates/index.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p>Search for a bus stop name here.</p>
<p>Search for a bus stop name here, or try <a href="/nearby">autolocation</a>.</p>
<form action="/" method="get">
<input name="search" type="text" />
<input type="submit" value="Search">
Expand All @@ -14,8 +14,9 @@
<% if result["stopIndicator"] %>
(<%= result["stopIndicator"] %>)
<% end %>
</a> <br />
</a> (<a href="http://maps.google.com/maps?z=16&q=loc:<%= result["lat"]%>+<%= result["lng"]%>">map</a>)
<% if result["towards"] %>
<br />
Towards <%= result["towards"].downcase.gsub(/\b\w/){$&.upcase} %>
<% end %>
<br />
Expand Down
6 changes: 6 additions & 0 deletions templates/nearby.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>Determining location...</p>
<script>
navigator.geolocation.getCurrentPosition(function(location) {
window.location = ("/?lat=" + location.coords.latitude + "&lon=" + location.coords.longitude);
});
</script>

0 comments on commit b14b87d

Please sign in to comment.