问题在MySQL数据库中存储纬度和经度值 [英] Problem Storing Latitude and Longitude values in MySQL database
问题描述
我想将从Google Maps GeoCoding API中取得的纬度和经度值存储在MySQL数据库中。
12.9274529
77.5905970
当我想将其存储在数据库(数据类型为float)时,它会将其浮动并以如下格式保存:
12.9275
77.5906
我使用了错误的数据类型吗?如果是的话,我应该使用什么数据类型来存储经度和纬度值?
更新:
由Allin请求的CREATE TABLE
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,
`````````varchar(50)DEFAULT NULL,
PRIMARY KEY(`id`),
UNIQUE KEY`serial`(`serial`)
)ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 3;
您需要使用 decimal ,如果你不希望这些数字是近似值的话。
定点(精确值)类型
DECIMAL和NUMERIC类型存储
确切的数字数据值。这些类型
用于
保存准确的精确度,例如
和货币数据。 使用DECIMAL(10,7) / code>。其中
。 (这意味着在点之前将是10
是数字中的总位数,7
是<$ c $之后的位数C>3
数字。)
根据需要调整这些数字。 请看看我在答案中早先链接的手册条目。
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 ;
解决方案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)
. Where10
is the total number of digits in the number and7
is the number of digits after the.
. (This means that before the dot will be3
digits.)Adjust these numbers as needed. Also please take a look at the manual entry I linked earlier in the answer.
这篇关于问题在MySQL数据库中存储纬度和经度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!