Use MySQL spatial ability or lat-long lookups for Rails 3 "Store Locator" functionality?
I have a Rails 3 application and I have a couple of use cases to support.
Store Locator given an address or zip code.
Graphical display of where users are originating from.
On the first one, I need speed and accuracy for a "Where to Buy" type of function.
On the second one, I don't need super accurate data, taking whatever the IP service returns is good enough. I basically want to put up a Google map with a bunch of pins stuck in it.
I have zero experience in this right now. I'm happy to write Javascript, and I have installed GeoKit to do the geocoding (a word which I can spell but which I don't really understand yet). I appreciate any help you can offer.
I know MySQL has spatial extensions and the updates look like they would be really simple. Maybe this is a split decision where I use the spatial information to store the sto开发者_如何学Cre locations, then just get the location of a user from their IP address.
For the site usage use case, does it make more sense to scan a log file live, or to store hits in a database?
Geocoding is converting something like an address or zip code to lat/long coordinates.
I use Google's Geocoding API, though the limit on the number of requests/day is 2,500 unless you're a Google Maps API Premier user. It's very fast (nearly every request my Rails app makes takes less than 0.1 seconds) and accurate. Have a read though, as there's more info on what geocoding is.
I use the lat/long returned from geocoding to query my MySQL database, which is a somewhat-expensive query but still executes very quickly (again, less than 0.1 seconds) over my reasonably small table (around 10,000 objects with lat/long values). Here's a query I use (replace {LAT} and {LNG} with your lat and long values):
SELECT
whatever, 3963.191 * ACOS((SIN(RADIANS({LAT}))*SIN(RADIANS(lat))) +(COS(RADIANS({LAT}))*cos(RADIANS(lat))*COS(RADIANS(lng)-RADIANS({LNG})))) AS distance
FROM things
HAVING distance < 30
ORDER BY distance;
精彩评论