简体   繁体   English

在 MySQL 数据库中存储纬度和经度值时出现问题

[英]Problem Storing Latitude and Longitude values in MySQL database

I want to store the values of latitude and longitude fetched from Google Maps GeoCoding API in a MySQL database.我想将从谷歌地图地理编码 API 获取的纬度和经度值存储在 MySQL 数据库中。 The values are in float format.这些值采用浮点格式。

12.9274529 12.9274529

77.5905970 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 12.9275

77.5906 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这是 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,
  `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. DECIMAL 和 NUMERIC 类型存储精确的数字数据值。 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) .使用DECIMAL(10,7) Where 10 is the total number of digits in the number and 7 is the number of digits after the .其中10是数字的总位数, 7是 . 后面的位数. . . (This means that before the dot will be 3 digits.) (这意味着点之前将是3位数字。)

Adjust these numbers as needed.根据需要调整这些数字。 Also please take a look at the manual entry I linked earlier in the answer.查看我之前在答案中链接的手动条目。

MySQL has special types for GIS applications. MySQL 具有用于 GIS 应用的特殊类型。

Use the point type and see:使用point类型并查看:

http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html http://dev.mysql.com/doc/refman/5.0/en/spatial-extensions.html

For a general discussion see: http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html有关一般性讨论,请参阅: http://dev.mysql.com/tech-resources/articles/4.1/gis-with-mysql.html

Some guys made a special UDF for computing distances between points on a sphere (ie earth)有些人制作了一个特殊的 UDF 来计算球体(即地球)上的点之间的距离
See: http://www.lenzg.net/archives/220-New-UDF-for-MySQL-5.1-provides-GIS-functions-distance_sphere-and-distance_spheroid.html请参阅: http://www.lenzg.net/archives/220-New-UDF-for-MySQL-5.1-provides-GIS-functions-distance_sphere-and-distance_spheroid.html

Here's a howto: http://howto-use-mysql-spatial-ext.blogspot.com/2007/11/using-circular-area-selection.html这是一个方法: http://howto-use-mysql-spatial-ext.blogspot.com/2007/11/using-circular-area-selection.html

use double使用double

float lacks the necessary precision to save that number of digits after the decimal point. float缺乏必要的精度来保存小数点后的位数。 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. double ,虽然并不总是保证所有数字都有 7 位小数,但小数点左侧的数字不超过 8 位,因此应该适合您的需要。

The optimal setup in my experience is DOUBLE(11,8), keep in mind that lat/lng could be > 99根据我的经验,最佳设置是 DOUBLE(11,8),请记住 lat/lng 可能 > 99

Use Double使用双

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

Decimal (10,8) is more than enough.十进制 (10,8) 绰绰有余。 Some GPS devices provide more accurate position.一些 GPS 器件提供更精确的 position。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM