Problem Storing Latitude and Longitude values in M

2020-02-17 06:03发布

I want to store the values of latitude and longitude fetched from Google Maps GeoCoding API in a MySQL database. The values are in float format.

12.9274529

77.5905970

And when I want to store it in database (which is datatype float) it rounds up float and store it in following format:

12.9275

77.5906

Am I using the wrong datatype? If yes then what datatype should I be using to store latitude and longitude values?

Update :

here is the CREATE TABLE as requestted by Allin

CREATE TABLE `properties` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `description` text,
  `latitude` float DEFAULT NULL,
  `longitude` float DEFAULT NULL,
  `landmark` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `serial` (`serial`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

7条回答
beautiful°
2楼-- · 2020-02-17 06:22

You need to use decimal if you don't want the numbers to be approximated.

Fixed-Point (Exact-Value) Types

The DECIMAL and NUMERIC types store exact numeric data values. These types are used when it is important to preserve exact precision, for example with monetary data.

And now the "here you go" answer:

Use DECIMAL(10,7). Where 10 is the total number of digits in the number and 7 is the number of digits after the .. (This means that before the dot will be 3 digits.)

Adjust these numbers as needed. Also please take a look at the manual entry I linked earlier in the answer.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-17 06:26

The optimal setup in my experience is DOUBLE(11,8), keep in mind that lat/lng could be > 99

查看更多
相关推荐>>
4楼-- · 2020-02-17 06:31
倾城 Initia
5楼-- · 2020-02-17 06:33

Alter your table so it's a double precision float instead of a single precision float:

alter table properties modify latitude double, modify longitude double;
查看更多
甜甜的少女心
6楼-- · 2020-02-17 06:34

use double

float lacks the necessary precision to save that number of digits after the decimal point. double, although not always guaranteed to have 7 decimal places for all numbers, will have where there are not more than 8 digits on the left of the decimal so should suit your needs.

查看更多
走好不送
7楼-- · 2020-02-17 06:36

Decimal (10,8) is more than enough. Some GPS devices provide more accurate position.

查看更多
登录 后发表回答