Skip to content

Commit

Permalink
Add geo search
Browse files Browse the repository at this point in the history
  • Loading branch information
excid3 committed Sep 12, 2017
1 parent 09eea20 commit 7f33fee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
13 changes: 12 additions & 1 deletion app/controllers/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ class TransactionsController < ApplicationController
# GET /transactions
# GET /transactions.json
def index
@transactions = Transaction.page(params[:page]).per(5)
@transactions = if params[:l]
sw_lat, sw_lng, ne_lat, ne_lng = params[:l].split(",")
center = Geocoder::Calculations.geographic_center([[sw_lat, sw_lng], [ne_lat, ne_lng]])
distance = Geocoder::Calculations.distance_between(center, [sw_lat, sw_lng])
box = Geocoder::Calculations.bounding_box(center, distance)
Transaction.within_bounding_box(box)
elsif params[:near]
Transaction.near(params[:near])
else
Transaction.all
end
@transactions = @transactions.page(params[:page]).per(5)
end

# GET /transactions/1
Expand Down
21 changes: 19 additions & 2 deletions app/javascript/packs/maps/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
document.addEventListener("turbolinks:load", function() {

var map = new GMaps({
div: '#map',
lat: 38.5816,
Expand All @@ -23,5 +22,23 @@ document.addEventListener("turbolinks:load", function() {
}
});

map.fitZoom();
var l = document.querySelector("#map").dataset.l;
if (l) {
var latlngs = l.split(',');
var southWest = new google.maps.LatLng(latlngs[0], latlngs[1]);
var northEast = new google.maps.LatLng(latlngs[2], latlngs[3]);
var bounds = new google.maps.LatLngBounds(southWest, northEast);
map.fitBounds(bounds, 0);
} else {
map.fitZoom();
}

document.querySelector("#redo-search").addEventListener("click", function(e) {
e.preventDefault();

var bounds = map.getBounds();
var location = bounds.getSouthWest().toUrlValue() + "," + bounds.getNorthEast().toUrlValue();

Turbolinks.visit(`/transactions?l=${location}`);
});
});
6 changes: 4 additions & 2 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
<%= link_to "Spark", root_path, class: "navbar-brand" %>

<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto mt-2 mt-md-0">
</ul>
<%= form_with(url: transactions_path, class: "form-inline my-2 my-lg-0 mr-auto", local: true, method: :get) do |f| %>
<%= f.text_field :near, class: "form-control mr-sm-2", placeholder: "Search" %>
<%= f.submit "Search", class: "btn btn-outline-secondary my-2 my-sm-0" %>
<% end %>

<ul class="navbar-nav">
<li class="nav-item"><%= link_to "What's New", announcements_path, class: "nav-link #{unread_announcements(current_user)}" %></li>
Expand Down
3 changes: 2 additions & 1 deletion app/views/transactions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</div>

<div class="col">
<%= tag.div nil, id: 'map', data: { transactions: @transactions.to_json(methods: [:address]) } %>
<%= tag.div nil, id: 'map', data: { transactions: @transactions.to_json(methods: [:address]), l: params[:l] } %>
<%= link_to "Redo search in this area", "#", id: "redo-search" %>
</div>
</div>

0 comments on commit 7f33fee

Please sign in to comment.