Search within a bounded box on a related model using Geocoder Gem - ruby-on-rails

In my search action, I am building up a query to list activities. I would like to be able to limit those to activities whose location is within 5 miles of a centre point and order the activities by distance from centre point.
Its simple enough to get a list of locations:
Location.within_bounding_box(Geocoder::Calculations.bounding_box([location[:latitude], location[:longitude]], 20))
but how can i search the activities model using bounding_box and order by distance?

Related

Rails geocoder find near locations within different radius

I want to find close locations for an user. Users table have their latitude and longitude.
Locations table have their latitude and longitude. Also locations have a radius attribute. I want to find locations for an user, that the user is inside those locations within specific radius. I can't do Location.near([user.latitude, user.longitude], radius), because all locations have different radius.
Also I cannot do Location.all.to_a.select {|l| u.distance_from([l. latitude, l. longitude]) < l.radius}, because it'd be inefficiently. It must be placed in the main page. Now I have about 1k locations and 1k users.
How can I do that? I use gem geocoder, but I can use something else.

Finding nearby addreses by geo location in rails

I have stored users - including their addresses - and need to find all users who live within a certain distance of a specific location.
I am using geocoder and have stored longitude and latitude in the Users table.
How do I find these user?
To do a query based on lat/lon, do this:
User.near([39.41, 90.23], 10)
That will find users within 10 miles of the lat/lon pair.
Here are the docs: https://github.com/alexreisner/geocoder

Maps API - city-block resolution?

'City Block' definition: The area (often a rectangle) bounded by 4 intersecting streets:
http://gyazo.com/46e1e6f0bbe97db8dd7dd19b0f38d016
Is there a convenient way of working out the nearest CITY BLOCK to a long,lat location?
If so, is there a way of getting the long,lat for, say, the lower left and upper right points of the 'City Block'?
I have a preference for a solution in Open Street Maps.
The OverpassAPI has an around query that allows you to get specific objects (for example ways tagged as highway=residential) around a given position. You still have to determine if and which of those streets define a city block and calculate their intersections (there should be a node at every intersection) in order to get lan,lon of those intersections.
You may also ask this question at the OSM help site.

Rails Finding towns within a 10 mile radius of postcode. Google maps API

I'm using the geocoder gem and was wondering if there is a way to get all cities within a radius (with center coordinates).
Something like
city = location.cities(100) #all cities within 100km
There seems to be a PHP solution, but I'm not sure how it can be used in my case.
Thanks!
I don't believe GoogleMap API offers spatial search. Geonames.org site publishes such a Web service -- the associated Geonames gem doc has a specific example for finding places near a lat/long point:
places_nearby = Geonames::WebService.find_nearby_place_name 43.900120387, -78.882869834

How to find GPS-coordinates within a bounding circle

I have a series of GPS coordinates in decimal dotted format, multiplied by 1.000.000. For example a latitude of 51.1 and a longitude of 4.1 would be saved as Y 51100000 and X 4100000. These coordinates are saved in an SQlite 3 database.
Using Ruby 1.9.2 and Rails 3.0.8, I need to be able to get all records that are within a certain radius of a certain center point. For instance, given a center point of latitude 51 and longitude 4, I need to find all records within a 10 kilometer radius.
This article explains pretty well how to perform a query to get those records, but SQlite does not seem to support the mathematical functions that are used: http://www.movable-type.co.uk/scripts/latlong-db.html
Is there any other way I would be able to retrieve the proper records from the database that does not involve iterating through the entire table?
Thanks!
Unless you MUST use SQLite, try it with Sphinx:
I would convert GPS coordinates to lat/lng coordinates, and then use Sphinx (there is gem thinking-sphinx for rails: http://freelancing-god.github.com/ts/en/). With Sphinx you can search points within a given circle with Sphinx's function: #geodist
A brilliant example of how to do it you will see here: http://joeyschoblaska.com/blog_posts/220-thinking-sphinx-searching-by-location-and-keyword

Resources