Correct datatype for latitude and longitude? (in activerecord)

前端 未结 6 1362
借酒劲吻你
借酒劲吻你 2021-01-30 03:07

Should I store latitude and longitude as strings or floats (or something else)?

(I\'m using activerecord / ruby on rails, if that matters).

Update:

Mysql

相关标签:
6条回答
  • 2021-01-30 03:22

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

    0 讨论(0)
  • 2021-01-30 03:24

    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 讨论(0)
  • 2021-01-30 03:40

    Since they're fixed precision, you should convert and store as integer for a significant performance improvement.

    (SEE http://www.postgresql.org/docs/9.1/static/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL)

    0 讨论(0)
  • 2021-01-30 03:41

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

    0 讨论(0)
  • 2021-01-30 03:44

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

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

    0 讨论(0)
  • 2021-01-30 03:44

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

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

    0 讨论(0)
提交回复
热议问题