views:

498

answers:

3

I have an application that uses the Google Maps API to geocode distances between lat/long pairs as a way of displaying people near to you on your phone (currently Android, working on iPhone). The issue is that even with a test group of 40 users, we are taking upwards of 10 seconds to do our calculations and send the results back to the users. While 10 seconds sounds like a long time, it's not really an issue as far as the client app goes because it's not a real-time update of people's locations (the updates occur every few minutes). Obviously this is a problem though since we'd obviously love to ramp up to tens or even hundreds of thousands of users. I'm curious if anyone else has any experience in this arena in regards to using the Google Maps API for calculating distances between points for large volumes of data?

As an aside, we're using Rails on the server, which is where all of the location calculations are occurring. The phone(s) are merely displaying the maps and updating the server with lat/long coordinates.

+3  A: 

You do not need the google maps API to calculate distances when you already have lat/lon coordinates. Calculating the great-circle distance can be done using haversine or vincenty formula.

Edit: If I understand your problem correctly (finding close locations to one given location in 10,000 records) I can only recommend using some geo library for this purpose. Calculating 10k distances is a bad idea when more requests are coming in. You should definitely look into smarter algorithms for that (a quad tree seems practical).

Nikolai Ruhe
+1  A: 

Take a peek at Geokit, it is a Ruby Gem and Rails plugin to do what you want and more, I think you'll be very happy with the speed and features as well.

revgum
Actually, in discussing this with my partner (he did the server work, I did the phone work), we are apparently already using GeoKit. If you are vouching for its performance then I wonder if our usage of it is poorly implemented. In my partner's defense though, we were under a pretty intense deadline at the time :)
MattC
A group of 40 users shouldn't take 10 seconds for a result at all. There must be something else tying up the response.. I have around 150 rows of a model having latitude and longitude and use :origin as a zipcode, and order by :distance.. my results come return in milliseconds.
revgum
We're thinking of tying it down to only grabbing users within the same zipcode, but there's obviously problems with that too (Cities with multiple zips, being on zipcode borders). We need to come up with a fast way to filter out other users based entirely on a borderless methodology.
MattC
Using zipcode or lat/lng (if you have it) as the :origin, use :within for some distance threshold possibly (25 miles, or some value you've determined to be practical for your users?) , you can sort by :distance and find the rows that are closest.. Anyway, just some generic ideas.
revgum
I'm also using geokit and it not as long ... maybe you could paste some code ?
Mike
I'm just getting my feet wet in the server code so once I figure out what's going on tonight after work I'll update the question with some code
MattC
So in looking at the code that is doing the distance calcs, we are using google geocoding to do it. I'm going to spend tonight and tomorrow night attempting to migrate it to using geokit locally for all distance calcs.
MattC
+1  A: 

You might want to considered converting lat long to a localized equidistant projection before doing any calculations if you are covering a specific area. Or more simply if covering global areas, convert the lat long to the two nearest UTM zones, storing two sets of X Y coordinates and the two UTM zones IDs. Then you can select records on UTM zone matches first, and carry out your calculations against that subset using the projected coordinates second (which will be significantly faster than calculating distance from lat long).

Brett
You have totally blown my mind. :D
MattC