Skip to content

Commit

Permalink
Add an around bus stop search using HTML5 geolocation API
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamuteki committed Apr 25, 2016
1 parent 146e8a1 commit ebcad9a
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
//= require bootstrap-sprockets
//= require underscore-min
//= require gmaps/google
//= require turbolinks
// require turbolinks
//= require_tree .
19 changes: 19 additions & 0 deletions app/assets/javascripts/geolocation.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
$ ->
$("input[data-geolocation]").click (e) ->
return if ($('#q').val() != '') # keyword search
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(success, error)
else
console.warn('Navigator.geolocation not supported.')
false

success = (position) ->
lat = position.coords.latitude
long = position.coords.longitude
window.location.href = '/bus_stops/?position=' + lat + ',' + long

error = (err) ->
console.warn('ERROR(' + err.code + '): ' + err.message)
9 changes: 8 additions & 1 deletion app/controllers/bus_stops_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
class BusStopsController < ApplicationController
def index
session[:q] = params[:q]
@bus_stops = BusStop.where("name like '%#{params[:q]}%'").limit(100)
@bus_stops = []
if params[:q] then
@bus_stops = BusStop.where("name like '%#{params[:q]}%'").limit(100)
elsif params[:position]
latitude = params[:position].split(',')[0].to_f
longitude = params[:position].split(',')[1].to_f
@bus_stops = BusStop.near([latitude, longitude], 20000).limit(25)
end
@hash = Gmaps4rails.build_markers(@bus_stops) do |bus_stop, marker|
marker.lat bus_stop.latitude
marker.lng bus_stop.longitude
Expand Down
1 change: 1 addition & 0 deletions app/models/bus_stop.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class BusStop < ActiveRecord::Base
has_and_belongs_to_many :bus_route_infos
reverse_geocoded_by :latitude, :longitude
def address
Geocoder.address("#{self.latitude},#{self.longitude}")
end
Expand Down
8 changes: 8 additions & 0 deletions app/views/application/_bus_stop_search_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= form_tag bus_stops_path, :method => 'get' do %>
<div class="input-group">
<%= text_field_tag :q, params[:q] || session[:q], placeholder: 'バス停名を省略すると周辺を検索します。', class: 'form-control' %>
<span class="input-group-btn">
<%= submit_tag '検索', name: nil, class: 'btn btn-primary', data: { geolocation: '' } %>
</span>
</div>
<% end %>
9 changes: 1 addition & 8 deletions app/views/bus_route_infos/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<div class="row page-top-search-box">
<div class="col-sm-4">
<%= form_tag bus_stops_path, :method => 'get' do %>
<div class="input-group">
<%= text_field_tag :q, session[:q], placeholder: 'バス停名を入力してください。', class: 'form-control' %>
<span class="input-group-btn">
<%= submit_tag "検索", name: nil, class: 'btn btn-primary' %>
</span>
</div>
<% end %>
<%= render partial: 'bus_stop_search_form' %>
<h1><%= @bus_route_info.line_name %></h1>
<p>
<%= "#{@bus_route_info.operation_company}" %><br />
Expand Down
9 changes: 1 addition & 8 deletions app/views/bus_stops/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<div class="row page-top-search-box">
<div class="col-sm-4">
<%= form_tag bus_stops_path, :method => 'get' do %>
<div class="input-group">
<%= text_field_tag :q, params[:q], placeholder: 'バス停名を入力してください。', class: 'form-control' %>
<span class="input-group-btn">
<%= submit_tag "検索", name: nil, class: 'btn btn-primary' %>
</span>
</div>
<% end %>
<%= render partial: 'bus_stop_search_form' %>
<h1>検索結果</h1>
<ol class="find-result">
<% @bus_stops.each do |bus_stop| %>
Expand Down
9 changes: 1 addition & 8 deletions app/views/bus_stops/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<div class="row page-top-search-box">
<div class="col-sm-4">
<%= form_tag bus_stops_path, :method => 'get' do %>
<div class="input-group">
<%= text_field_tag :q, session[:q], placeholder: 'バス停名を入力してください。', class: 'form-control' %>
<span class="input-group-btn">
<%= submit_tag "検索", name: nil, class: 'btn btn-primary' %>
</span>
</div>
<% end %>
<%= render partial: 'bus_stop_search_form' %>
<h1><%= @bus_stop.name %></h1>
<p><%= link_to @bus_stop.address + '付近', "http://maps.google.co.jp/maps?q=#{@bus_stop.latitude},#{@bus_stop.longitude}" %></p>
<ol>
Expand Down
9 changes: 1 addition & 8 deletions app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
</p>
</div>
<div class="col-sm-offset-4 col-sm-4 text-center">
<%= form_tag bus_stops_path, :method => 'get' do %>
<div class="input-group">
<%= text_field_tag :q, params[:q], placeholder: 'バス停名を入力してください。', class: 'form-control' %>
<span class="input-group-btn">
<%= submit_tag "検索", name: nil, class: 'btn btn-primary' %>
</span>
</div>
<% end %>
<%= render partial: 'bus_stop_search_form' %>
</div>
<div class="col-sm-offset-4 col-sm-4">
<ul class="reference-info">
Expand Down

0 comments on commit ebcad9a

Please sign in to comment.