Home >Database >Mysql Tutorial >How Can I Precisely Store Latitude/Longitude Data with 8 Decimal Places in MySQL?

How Can I Precisely Store Latitude/Longitude Data with 8 Decimal Places in MySQL?

Linda Hamilton
Linda HamiltonOriginal
2024-12-01 11:38:09948browse

How Can I Precisely Store Latitude/Longitude Data with 8 Decimal Places in MySQL?

Precise Storage of Latitude/Longitude Data with 8 Decimal Places in MySQL

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn