I've been using GeoKit for Rails and it works 开发者_C百科great. However, I noticed that when pulling alot of listings it is VERY slow in loading.
I'm wondering if it is because it is querying google or some external source before returning the results. If thats the case, is there a way to use raw lat lng coordinates (which I have) to simply calculate distance internally?
Or a simply ruby formula that will do this for me?
Distance-wise:
Geokit-gem provides a method to calculate the distance between two points:
# model has acts_as_mappable
Model.distance_between obj1, obj2, :units => :kms, :formula => :sphere
Finder-wise:
Geokit-rails (a plugin) has a finder that generates SQL (for mysql, postgres, and SQL server) to calculate distances, letting you find rows within a certain distance:
Model.find_within(100, :units => :kms)
It calculates distances for all rows, but doesn't need to query external resources. If your table is truly huge it might help (but I'm not so sure that it would) to narrow it down by pre-calculating lat/lng ranges first:
Model.where(:lat => (x-0.05)..(x+0.05), :lng => (y-0.05)..(y+0.05)).find_within(z, :units => :kms)
If that still doesn't help you're gonna have to write some customised SQL. The sql distance calculations for geokit-rails are here: https://github.com/andre/geokit-rails/tree/master/lib/geokit-rails/adapters
You can use the formulas for spheres to calculate the distance of two points on the surface.
Here is some JavaScript implementation for calculating the distance between latitudes/longitudes (and some other usefull stuff). Should not be too much of brain-work to translate to ruby ;-)
http://www.movable-type.co.uk/scripts/latlong.html
精彩评论