Rails geocoder find near locations within different radius - ruby-on-rails

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.

Related

lat lng to neighborhood info

Is there a dynamic hierarchical data source out there that I can use to identify a lat lng point into a neighborhood?
For instance, if I was in Manhattan, it would recognize that I'm in Chinatown, Manhattan, New York City in that order. And if I was in a less densely populated area it would just put me into a neighborhood that would span a larger area. It can be a bit fuzzy in this concept.
Ultimately I want to group people into their nearest neighobrhood given evenly sized neighborhood population.
I know that zip codes can roll up into a metro area, but I wonder if there is something that's more granular or more dynamic.
Google's geocoding API can give a variety of levels of detail about a location. It varies by region, country, and even at state/local levels but you should be able to get close to what you're looking for.

Are geolocation apps based on latitude and longitude data?

Are most web and smartphone applications that show you how far other users are from you essentially based on a user's latitude and longitude? That is, do these apps basically derive a user's latitude and longitude from their country and postal code and then use an algorithm to create a sorted list of all users who are near them, closest first? I believe the answer is "yes" but I want to make sure before I build this feature into my Django application.
For those countries that don't use postal codes, I would imagine the latitude and longitude are derived from the city/region/country tuple they reside in.
Yes, they map IP to lat/long, and lat/long to city, and city to country, unless they have access to a GPS device/sensor for the exact position (+/- 1m).
The thing is, if you have an ip, you can convert that into a number (biginteger)
74.88.21.55 ==> (a.b.c.d) ==> d * 255^0 + c * 255^1 + b * 255^2 +a * 255^3 = x
then you can query a database.
All you need is a lookup-table:
T_Lookup
IP_Range_Start IP_Range_End Latitute Longitue
Then you can query like this:
SELECT Latitude, Longitude FROM T_Lookup
WHERE x BETWEEN IP_Range_Start AND IP_Range_End
Then you can calculate the distance between the two points using the haversine formula.
You can grab C code here:
http://aimbots.net/tutorials/7680-getting-country-ip-address.html
And an IP to country csv you find here:
http://ip-to-country.webhosting.info/node/view/6
Apps that display the locations of users relative to other users generally gather their data from either GPS data or IP address location data.
This Wiki article provides a nice explanation of geolocation.
They use the Galactic Coordinate system, which does in fact use longitude, l, and latitude, b.
http://en.wikipedia.org/wiki/Galactic_coordinate_system

Search within a bounded box on a related model using Geocoder Gem

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?

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

Rails google map check if point lies within certain radius

I have a user model which stores the lat and log of a user in latitude and longitude field.
Now I have a arbitrary point (lat and longitude).
Now what is the best way to find all the users which lies in the radius of 10kms of that arbitrary point.
Does geocoder gem does it?
Thanks
Geocoder does seem to have what you need. You could also roll your own.
Yup Geocoder have this feature ie to check if it any point (latitude, longitude) lies within certain radius of certian point.
Lets say an arbitrary object of model X with fields latitude = x and longitude = y be obj
Now to find any similar object within m miles radius from obj we can do
obj.nearbys(m)
This query will return all X's which lies within x miles radius form obj

Resources