mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-01-03 23:17:49 -05:00
Sort nearby members by distance
Also removed distance/units from the places/show search, since you can adjust nearness by zooming etc. At this point the "members near here" stuff at the bottom of the page exists mostly for accessibility and to give additional detail that we don't currently show in the popups on the map. So we're not using distance/units to search for members near here anymore, but instead are just finding the 30 nearest members to the specified location, and showing them in order of nearness.
This commit is contained in:
@@ -14,39 +14,17 @@ class PlacesController < ApplicationController
|
||||
def show
|
||||
@place = params[:place]
|
||||
|
||||
if !params[:distance].blank?
|
||||
@distance = params[:distance]
|
||||
else
|
||||
@distance = 100
|
||||
end
|
||||
# calculate location just once, rather than using @place later
|
||||
# sadly we also do this in the javascript, which means we're making
|
||||
# the same query twice, but I'm not sure how to avoid that.
|
||||
location = Geocoder.search(Geocoder::Query.new(@place, :params => {limit: 1}))
|
||||
|
||||
if params[:units] == "mi"
|
||||
@units = :mi
|
||||
else
|
||||
@units = :km
|
||||
@nearby_members = []
|
||||
if location
|
||||
coords = [location[0].data['lat'], location[0].data['lon']]
|
||||
@nearby_members = Member.near(coords, 1000, :units => :km).sort_by { |x| x.distance_from(coords) }.first(30)
|
||||
end
|
||||
|
||||
query = Geocoder::Query.new(
|
||||
@place, :distance => @distance, :units => @units, :params => {limit: 1}
|
||||
)
|
||||
location = Geocoder.search(query)
|
||||
if location && location[0] && location[0].coordinates
|
||||
@latitude, @longitude = location[0].coordinates
|
||||
if @distance
|
||||
@sw_lat, @sw_lng, @ne_lat, @ne_lng = Geocoder::Calculations.bounding_box(
|
||||
[@latitude, @longitude],
|
||||
@distance,
|
||||
{ :units => @units }
|
||||
)
|
||||
end
|
||||
else
|
||||
@latitude, @longitude = [0, 0]
|
||||
@sw_lat, @sw_lng, @ne_lat, @ne_lng = [0,0,0,0]
|
||||
flash[:alert] = "Sorry, our map provider can't find this location."
|
||||
end
|
||||
|
||||
@nearby_members = @place ? Member.near(@place, @distance, :units => @units) : []
|
||||
|
||||
respond_to do |format|
|
||||
format.html # show.html.haml
|
||||
format.json { render :json => @nearby_members.to_json(:only => [:id, :login_name, :location, :latitude, :longitude]) }
|
||||
|
||||
@@ -1,23 +1,20 @@
|
||||
-content_for :title, @place
|
||||
|
||||
= form_tag(search_places_path, :method => :get, :class => 'form-inline') do
|
||||
= label_tag :place, "Change location:", :class => 'control-label'
|
||||
= text_field_tag :new_place, @place
|
||||
= submit_tag "Search", :class => 'btn btn-primary'
|
||||
|
||||
%div#map{ :style => "height:300px"}
|
||||
|
||||
%h3
|
||||
= Growstuff::Application.config.site_name
|
||||
members near #{@place}
|
||||
|
||||
= form_tag(search_places_path, :method => :get, :class => 'form-inline') do
|
||||
= label_tag :distance, "Find members within", :class => 'control-label'
|
||||
= text_field_tag :distance, @distance, :class => 'input-mini'
|
||||
= select_tag :units, options_for_select({"miles" => :mi, "km" => :km}, @units), :class => 'input-small'
|
||||
= label_tag :place, "of", :class => 'control-label'
|
||||
= text_field_tag :new_place, @place
|
||||
= submit_tag "Search", :class => 'btn btn-primary'
|
||||
|
||||
- if !@nearby_members.empty?
|
||||
%ul.thumbnails
|
||||
- @nearby_members.each do |member|
|
||||
%li.span2
|
||||
%li.span4.three-across
|
||||
= render :partial => "members/thumbnail", :locals => { :member => member }
|
||||
- elsif @place
|
||||
%p No results found
|
||||
|
||||
Reference in New Issue
Block a user