Rails google map check if point lies within certain radius - ruby-on-rails

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

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.

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

How do I find the latitude and longitude from Routeboxer for Google Maps API V3?

Thanks in advance for any help you can provide! I've reviewed several example sites, but I haven't been able to find a version of this code that matches what I'm trying to accomplish with my website (Ruby on Rails, 2.3.15, 1.8.7.)
My Goal:
In my website, I have a tool that lets users create a driving route. My goal is to give the user an option of clicking a "Show Nearby Points-of-Interest" checkbox before loading the map. If he does, I want the map to show POIs along the route.
Where I Am
I've implemented RouteBoxer, which I believe is my best option for finding Points of Interest along the route. Routeboxer is working for me; it draws boxes around any route I create. This is where I'm stuck, however. I've just got my route and a bunch of boxes on the screen. How can I actually get the latitude and longitude bounds from RouteBoxer? Then, how can I pass my data through RouteBoxer?
Details
I have a MySQL database named "myinfo" and a table in it named "masterlocations". Each location has an "ID", "Lat", "Long", "nickname", and other info.
In my map webpage controller, I played around and found that I could get a list of locations by hardcoding the latitude and longitude. The test looks like this:
#nearbylocations = Masterlocation.find(:all, :conditions => ["latitude > 25 AND latitude < 30"], :order => ['nickname asc'])
Next Steps / Advice
I think I need to figure out the latitude and longitude of the RouteBoxer polygon and then somehow see if any of my locations fall within this latitude and longitude. If they do, I then need to figure out a way so that the API creates markers for the POIs.
Do you have suggestions of how I can accomplish the above? I'm fairly new to both Ruby and Google Maps API V3, so sorry if I'm blowing past an obvious way of doing this. Any detail or code you can provide / link to would be fantastic! Thanks again for your time!
You first get boxes from router box
var boxes = routeBoxer.box(path, distance);
A box is just a series of lat/longs
for (var i = 0; i < boxes.length; i++) {
var northeast = boxes[i].getNorthEast();
var southwest = boxes[i].getSouthWest();
var lat_north = northeast.lat();
var long_east = northeast.lng();
var lat_south = southwest.lat();
var long_west = southwest.lng();
...
Now that you have the box bounds (the lat/longs that make it up), you can loop through all of your points to see if they reside within this box. To check this is just a simple 'does this point reside in this rectangle' algorithm (if point.x between box.left and box.right and point.y between box.top and box.bottom)
So summarily
Loop through boxes { Loop through coords { } }
Of course, this is just the start... This is a 'brute force' method where every point is checked against every box and depending on your route and number of boxes this can result in a huge number of permutations. There is ALOT of room for improvement using all kinds of algorithms. That was the fun part of this exercise when I did it - to use this or that; to do the crunching server side or client side...choices... Have fun...

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

using geokit or other ruby gem to calculate the center of a series of geo coordinates

I've been using the geokit and geokit-rails gem for rails for awhile but one question I haven't found answered is how to find the calculated aggregate center for a collection of points. I know how to calculate the distance between two points, but not more than 2.
My reason is, I have a series of points all in the same city... all things being perfect the city would have a center which I could just use, but some cities, say berlin do not have a perfect center. They have multiple centers, and I just want to use the whole list of places I have in my database to calculate a center for a particular distribution. Has anyone else had this problem?
Any tips? Thanks
Having never used Geokit before, the math behind this operation is relatively simple to implement yourself. Assuming these points consist of a latitude and a longitude, you just need the average latitude and average longitude for all the points. Once you have those two values, you've got your center point.
points = [[14, 19], [-5, 57], [23, -12]]
points.transpose.map{|c| c.inject{|a, b| a + b}.to_f / c.size}
Likewise, if these points are Geokit::LatLng objects instead of a 2-dimensional array, you can just map their lat and lng values simply by calling #to_a on them beforehand.
points.map(&:to_a).transpose.map{|c| c.inject{|a, b| a + b}.to_f / c.size}

Resources