So, it seems this is the action that needs changing:
@latitude = params[:latitude].to_f * Math::PI / 180
@longitude = params[:longitude].to_f * Math::PI / 180
@users = Location.search :geo => [@latitude * Math::PI / 180, @longitude *
Math::PI / 180], :with => {:geodist => 0.0..200_000.0}, :order => "geodist ASC"
I would switch it to:
latitude = params[:latitude].to_f * Math::PI / 180
longitude = params[:longitude].to_f * Math::PI / 180
location_ids = Location.search_for_ids(
:geo => [latitude, longitude],
:with => {:geodist => 0.0..200_000.0},
:order => "geodist ASC”
)
@users = User.where(:location_id => location_ids)
Note the following:
* I’ve removed the double radians conversion
* I’m using the search_for_ids method to avoid instantiating unnecessary
location objects.
* Instance variables are only useful for objects used in the views, so I’ve
made a presumption and just set @users, everything else is a local variable
instead.
If all you want is the user to enter their zip code for the search, then it
would be more like this:
location = Location.find_by_zipcode params[:zipcode]
# if lat/lng are stored as degrees
latitude = location.latitude * Math::PI / 180
longitude = location.longitude * Math::PI / 180
location_ids = Location.search_for_ids(
:geo => [latitude, longitude],
:with => {:geodist => 0.0..200_000.0},
:order => "geodist ASC”
)
@users = User.where(:location_id => location_ids)
Hope this helps!
—
Pat
On 22 Jan 2014, at 6:09 am, C Wilson <[email protected]> wrote:
> Hey Pat, you assisted me with the radians/degrees issue. I was looking for a
> clearer idea than what Nicholas suggested for the part I needed advice on.
>
> I have TS setup to find locations based on latitude/longitude that are near
> one another. This part functions as expected.
>
> The next step that I am not clear on is how to setup so that if I perform a
> location search using a zip code from the Locations table to have it return
> users who are within the specified distance.
>
> I am building a app so users can find nearby users using their zip code. I
> want users to enter any zip code and it will return users within 200km.
>
> I am not sure if there is more required from me in the searches controller
> that will make the search perform how I would like when users a zip code
> within the search box.
>
>
> Location.search :geo => [33.89 * Math::PI / 180, -83.95 * Math::PI / 180],
> :with => {:geodist => 0.0..200_000.0}, :order => "geodist ASC"
> Sphinx Query (338.5ms) SELECT GEODIST(0.5914920835008783,
> -1.4652039070492398, latitude, longitude) AS geodist, * FROM `location_core`
> WHERE `geodist` BETWEEN 0.0 AND 200000.0 AND `sphinx_deleted` = 0 ORDER BY
> `geodist` ASC LIMIT 0, 20
> Sphinx Found 983 results
> Location Load (0.5ms) SELECT `locations`.* FROM `locations` WHERE
> `locations`.`id` IN (12828, 12848, 12882, 12851, 12852, 12853, 12854, 12855,
> 12859, 12830, 12856, 12857, 12822, 12834, 12836, 12838, 12898, 12899, 12900,
> 12901)
> => [#<Location id: 12828, zipcode: "30017", city: "Grayson", state: "GA",
> latitude: 33.89, longitude: -83.95, created_at: nil, updated_at: nil>,
> #<Location id: 12848, zipcode: "30039", city: "Snellville", state: "GA",
> latitude: 33.85, longitude: -84.0, created_at: nil, updated_at: nil>,
> #<Location id: 12882, zipcode: "30078", city: "Snellville", state: "GA",
> latitude: 33.85, longitude: -84.0, created_at: nil, updated_at: nil>,
> #<Location id: 12851, zipcode: "30042", city: "Lawrenceville", state: "GA",
> latitude: 33.94, longitude: -83.99, created_at: nil, updated_at: nil>,
> #<Location id: 12852, zipcode: "30043", city: "Lawrenceville", state: "GA",
> latitude: 33.94, longitude: -83.99, created_at: nil, updated_at: nil>,
> #<Location id: 12853, zipcode: "30044", city: "Lawrenceville", state: "GA",
> latitude: 33.94, longitude: -83.99, created_at: nil, updated_at: nil>,
> #<Location id: 12854, zipcode: "30045", city: "Lawrenceville", state: "GA",
> latitude: 33.94, longitude: -83.99, created_at: nil, updated_at: nil>,
> #<Location id: 12855, zipcode: "30046", city: "Lawrenceville", state: "GA",
> latitude: 33.94, longitude: -83.99, created_at: nil, updated_at: nil>,
> #<Location id: 12859, zipcode: "30052", city: "Loganville", state: "GA",
> latitude: 33.83, longitude: -83.89, created_at: nil, updated_at: nil>,
> #<Location id: 12830, zipcode: "30019", city: "Dacula", state: "GA",
> latitude: 33.98, longitude: -83.88, created_at: nil, updated_at: nil>,
> #<Location id: 12856, zipcode: "30047", city: "Lilburn", state: "GA",
> latitude: 33.88, longitude: -84.13, created_at: nil, updated_at: nil>,
> #<Location id: 12857, zipcode: "30048", city: "Lilburn", state: "GA",
> latitude: 33.88, longitude: -84.13, created_at: nil, updated_at: nil>,
> #<Location id: 12822, zipcode: "30011", city: "Auburn", state: "GA",
> latitude: 34.01, longitude: -83.83, created_at: nil, updated_at: nil>,
> #<Location id: 12834, zipcode: "30024", city: "Suwanee", state: "GA",
> latitude: 34.05, longitude: -84.07, created_at: nil, updated_at: nil>,
> #<Location id: 12836, zipcode: "30026", city: "North Metro", state: "GA",
> latitude: 34.0, longitude: -84.15, created_at: nil, updated_at: nil>,
> #<Location id: 12838, zipcode: "30029", city: "North Metro", state: "GA",
> latitude: 34.0, longitude: -84.15, created_at: nil, updated_at: nil>,
> #<Location id: 12898, zipcode: "30095", city: "Duluth", state: "GA",
> latitude: 34.0, longitude: -84.15, created_at: nil, updated_at: nil>,
> #<Location id: 12899, zipcode: "30096", city: "Duluth", state: "GA",
> latitude: 34.0, longitude: -84.15, created_at: nil, updated_at: nil>,
> #<Location id: 12900, zipcode: "30097", city: "Duluth", state: "GA",
> latitude: 34.0, longitude: -84.15, created_at: nil, updated_at: nil>,
> #<Location id: 12901, zipcode: "30098", city: "Duluth", state: "GA",
> latitude: 34.0, longitude: -84.15, created_at: nil, updated_at: nil>]
>
>
> user = User.first
> User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC
> LIMIT 1
> => #<User id: 1, email: "[email protected]", password_digest:
> "$2a$10$OuN0/fqWzy4ZTyIf4y94teIdnndAqwS.1V5zxgO9X7wW...", zip_code: "30052",
> birthday: "1986-08-25", name: nil, username: "admin", gender: "Male",
> ethnicity: nil, sexuality: "Straight", career: nil, education: nil, religion:
> nil, politics: nil, children: nil, height: nil, user_smoke: nil, user_drink:
> nil, about_me: nil, inches: nil, feet: nil, created_at: "2014-01-16
> 14:17:36", updated_at: "2014-01-16 14:17:36", auth_token:
> "8Zwh3HF6WnA8wthFZirkQw", password_reset_token: nil, password_reset_sent_at:
> nil, admin: nil, role: "admin", roles_mask: nil, age: nil, default_photo_id:
> nil, location_id: nil, time_zone: nil>
> 2.0.0-p353 :004 > user.location
> (15.4ms) SELECT COUNT(*) FROM `locations` WHERE (zipcode = '30052')
> Location Load (6.8ms) SELECT `locations`.* FROM `locations` WHERE (zipcode
> = '30052') ORDER BY `locations`.`id` ASC LIMIT 1
> => #<Location id: 12859, zipcode: "30052", city: "Loganville", state: "GA",
> latitude: 33.83, longitude: -83.89, created_at: nil, updated_at: nil>
>
>
> Location columns:
>
> t.string :zipcode
> t.string :city
> t.string :state
> t.float :latitude
> t.float :longitude
>
> User columns:
> t.string :email
> t.string :password_digest
> t.string :zip_code
> t.string :location_id
> t.string :time_zone
> t.string :birthday
> t.string :name
> t.string :username
> t.string :gender
> t.string :ethnicity
> t.string :sexuality
> t.string :career
> t.string :education
> t.string :religion
> t.string :politics
> t.string :children
> t.string :height
> t.string :user_smoke
> t.string :user_drink
> t.string :about_me
> t.string :inches
> t.string :feet
>
>
> class DistanceController < ApplicationController
> def search
> @latitude = params[:latitude].to_f * Math::PI / 180
> @longitude = params[:longitude].to_f * Math::PI / 180
> @users = Location.search :geo => [@latitude * Math::PI / 180, @longitude
> * Math::PI / 180], :with => {:geodist => 0.0..200_000.0}, :order => "geodist
> ASC"
> end
>
> def users
> @users = User.all
> end
>
> def index
> @users = User.all
> end
> end
>
>
> ThinkingSphinx::Index.define :location, :with => :active_record do
> indexes city
>
> has "RADIANS(locations.latitude)", :as => :latitude, :type => :float
> has "RADIANS(locations.longitude)", :as => :longitude, :type => :float
> end
> ThinkingSphinx::Index.define :user, :with => :active_record do
> indexes name, :as => :user, :sortable => true
> indexes zip_code
> has created_at, updated_at
> has location.id, :as => :location_id
> end
>
>
> <%= form_tag searches_path, method: :get do %>
> <p>
> <%= text_field_tag :search, params[:search] %>
> <%= button_tag "Search", name: nil %>
> </p>
> <% end %>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/thinking-sphinx.
> For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/groups/opt_out.