Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suitable mysql datatype to store Latitude and Longitude in separate fields

Tags:

types

mysql

I have stored Latitude and Longitude in separate fields. I am getting Lat & Lng coordinates from google and also used them to find nearby places using some formula. Now sometimes latitude and longitude value reach upto 15 decimal places like this --> 15.123456789000001. what would be the best datatype to store these values? Right now i am using Double to store Latitude and Longitude coordinates.

Thanks in Advance

like image 644
Suleman Siddiqui Avatar asked Aug 22 '11 07:08

Suleman Siddiqui


2 Answers

99% of the time, any floating-point data type of your choice should be sufficient.

Yes, I know they can't always precisely and accurately represent values out to 15 decimal places, but that doesn't matter. Lat/long to 15 decimal places is specifying a position with a (claimed) accuracy on the order of the width of a couple hydrogen atoms. You only need six decimal places to get accuracy to within a meter, which is sufficient for most cases. Very few should need more than centimeter-level accuracy (8 places) and 10 or even 100 meter accuracy will be good enough for many applications.

And, of course, that's all assuming that the original data source is that accurate in the first place. My phone's GPS rarely claims accuracy better than within 10m (and even then, half the time its map doesn't show my actual location within the 10m circle where it claims I should be), so storing a position to greater precision would be pointless and misleading. Granted, you may be using a more accurate data source than the cheap GPS receiver integrated into a cellphone, but, even so, it's unlikely to actually be accurate all the way down to a molecular scale.

like image 124
Dave Sherohman Avatar answered Jan 02 '23 20:01

Dave Sherohman


I've used DECIMAL data type.

CREATE TABLE IF NOT EXISTS `example` (
  `lat` decimal(18,15) DEFAULT NULL,
  `long` decimal(18,15) DEFAULT NULL 
);
like image 35
Daniel Luca CleanUnicorn Avatar answered Jan 02 '23 21:01

Daniel Luca CleanUnicorn