8

What would be the ideal way of keeping location coordinates as a model field inorder to use at Google Maps at Django?

Hellnar
  • 62,315
  • 79
  • 204
  • 279

4 Answers4

15

Here's the code I use for storing Google's Lat/Lon

lat = models.FloatField(_('Latitude'), blank=True, null=True)
lon = models.FloatField(_('Longitude'), blank=True, null=True)
silent1mezzo
  • 2,814
  • 4
  • 26
  • 46
  • 3
    Old post, but what is the "_" for infront of the "('latitude')? – Marcus Oct 18 '13 at 23:53
  • 7
    @Mhsmith21: _ is a common used shortcut for gettext-functions. E.G. `from django.utils.translation import ugettext as _`. See [here](https://docs.djangoproject.com/en/dev/topics/i18n/translation/) – sphere Nov 06 '13 at 12:55
13

Possible a better idea is to store in decimal field

lat = models.DecimalField(_('Latitude'), max_digits=10, decimal_places=8)
lng = models.DecimalField(_('Longitude'), max_digits=11, decimal_places=8)

see https://stackoverflow.com/a/12504340/1977197

Community
  • 1
  • 1
Sergey Telminov
  • 769
  • 7
  • 10
10

If you are only ever going to store/retrieve a pair of lat/lon coords a models.FloatField pair is probably fine. If you planned to do spatial queries, you should consider using some of the special fields provided by GeoDjango, which adds support for PostGIS and geographic data.

Zach Kelling
  • 52,505
  • 13
  • 109
  • 108
6

I'd think it's probably easier to keep the latitude and longitude as separate FloatFields

j_syk
  • 6,511
  • 2
  • 39
  • 56
  • What would be the implementation so I it is needed to add origin and destination? Do you think it should be added separately? – Elias Prado Sep 04 '22 at 18:52