[ACCEPTED]-Correct datatype for latitude and longitude? (in activerecord)-types

Accepted answer
Score: 156

This is what I use:

add_column :table_name, :lat, :decimal, {:precision=>10, :scale=>6}
add_column :table_name, :lng, :decimal, {:precision=>10, :scale=>6}

0

Score: 23

If you need to do more complex geographical 7 calculations, you can investigate PostGIS for Postgresql 6 or MySQL Spatial Extensions for MySQL. Otherwise, a float (double 5 precision might be a good idea) should work.

Edit: It 4 looks like the GeoRuby library includes a Rails 3 extension for working with the spatial/GIS 2 extensions for both of the aforementioned 1 databases.

Score: 22

If you're not using a spatially-enabled 4 database, Google Maps recommends using floats of size 3 (10,6). That gives you 6 digits after the 2 decimal - if you want more precision, you 1 can adjust accordingly.

Score: 3

I would suggest using floats, although it 3 doesn't really make that much of a difference. Floats 2 are easier to do calculations on if you 1 ever desire that in the future.

Score: 2

Generally you want Lat/Long stored in the 7 largest float type you have. At some latitudes 6 (eg: near the equator) very small changes 5 in longitude can equate to large differences 4 in terms of surface distance.

I suppose if 3 it is a field which you won't ever want 2 to do any math on, you could use a string. I'd 1 avoid that though.

More Related questions