Problem:
When dealing with map data, it's crucial to store Latitude/Longitude coordinates with sufficient precision. In this case, the requirement is to accommodate values with 8 decimal places.
Discussion:
The Google document referenced suggests using FLOAT(10, 6) fields, which allows up to 6 decimal places. To accommodate 8 decimal places, one might consider using FLOAT(10, 8). However, there is a more precise and appropriate solution.
Solution:
MySQL offers Spatial data types, and Point is a suitable single-value type for storing geographic coordinates with precision. The following example demonstrates its usage:
CREATE TABLE `buildings` ( `coordinate` POINT NOT NULL, /* Even from v5.7.5 you can define an index for it */ SPATIAL INDEX `SPATIAL` (`coordinate`) ) ENGINE=InnoDB;
To insert data, you can use an SQL statement like this:
INSERT INTO `buildings` (`coordinate`) VALUES (POINT(40.71727401 -74.00898606));
The Point data type ensures that coordinates are stored with high precision, preserving the 8 decimal places required for accurate map calculations.
The above is the detailed content of How Can I Precisely Store Latitude/Longitude Data with 8 Decimal Places in MySQL?. For more information, please follow other related articles on the PHP Chinese website!