Problem Storing Latitude and Longitude values in MySQL database

前端 未结 7 849
逝去的感伤
逝去的感伤 2020-12-24 03:19

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.92

相关标签:
7条回答
  • 2020-12-24 03:45

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

    0 讨论(0)
  • 2020-12-24 03:47

    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 ;
    
    0 讨论(0)
  • 2020-12-24 03:55

    MySQL has special types for GIS applications.

    Use the point type and see:

    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

    Some guys made a special UDF for computing distances between points on a sphere (i.e. earth)
    See: 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

    0 讨论(0)
  • 2020-12-24 03:55

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

    0 讨论(0)
  • 2020-12-24 04:03

    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;
    
    0 讨论(0)
  • 2020-12-24 04:05

    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.

    0 讨论(0)
提交回复
热议问题