Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store latitudes and longitudes in MySQL?

Tags:

mysql

I am trying to store latitude 39.8162994 and longitude -98.5576019 as floats but turns out be like 0.00000000000 and -98.55760192871, accordingly.

What's the problem? I am using float(15,11) as a data type.

like image 986
AleGore Avatar asked Jan 23 '23 11:01

AleGore


2 Answers

You should probably use DECIMAL(10,7) in order to be able to store a longitude value with 7 decimal places: ±179.1234567. The FLOAT data type is used to represent approximate numeric data values, where on the other hand you might prefer to store the exact values with the DECIMAL data type instead.

like image 176
Daniel Vassallo Avatar answered Jan 25 '23 00:01

Daniel Vassallo


I would recommend that you either use a DECIMAL type or store it as an integer with a known offset (say 10e-7).

Use at least as many digits precision as you need to keep:

DECIMAL(15,12)
like image 45
Kaleb Pederson Avatar answered Jan 25 '23 01:01

Kaleb Pederson