From 7d5643e30df73fb91d0da52b94e00a3af769eb0e Mon Sep 17 00:00:00 2001 From: Skud Date: Wed, 11 Sep 2013 12:39:16 +1000 Subject: [PATCH] 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. --- app/controllers/places_controller.rb | 38 ++++++---------------------- app/views/places/show.html.haml | 15 +++++------ 2 files changed, 14 insertions(+), 39 deletions(-) diff --git a/app/controllers/places_controller.rb b/app/controllers/places_controller.rb index 707319a5c..1f5b8f0ac 100644 --- a/app/controllers/places_controller.rb +++ b/app/controllers/places_controller.rb @@ -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]) } diff --git a/app/views/places/show.html.haml b/app/views/places/show.html.haml index 3283f9e11..833f6daaa 100644 --- a/app/views/places/show.html.haml +++ b/app/views/places/show.html.haml @@ -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