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:
Skud
2013-09-11 12:39:16 +10:00
parent 3cd48f751e
commit 7d5643e30d
2 changed files with 14 additions and 39 deletions

View File

@@ -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]) }

View File

@@ -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