What is the ideal data type to use when storing latitude / longitude in a MySQL database?

前端 未结 21 2406
梦如初夏
梦如初夏 2020-11-22 09:40

Bearing in mind that I\'ll be performing calculations on lat / long pairs, what datatype is best suited for use with a MySQL database?

相关标签:
21条回答
  • 2020-11-22 10:13

    Use DECIMAL(8,6) for latitude (90 to -90 degrees) and DECIMAL(9,6) for longitude (180 to -180 degrees). 6 decimal places is fine for most applications. Both should be "signed" to allow for negative values.

    0 讨论(0)
  • 2020-11-22 10:14

    A FLOAT should give you all of the precision you need, and be better for comparison functions than storing each co-ordinate as a string or the like.

    If your MySQL version is earlier than 5.0.3, you may need to take heed of certain floating point comparison errors however.

    Prior to MySQL 5.0.3, DECIMAL columns store values with exact precision because they are represented as strings, but calculations on DECIMAL values are done using floating-point operations. As of 5.0.3, MySQL performs DECIMAL operations with a precision of 64 decimal digits, which should solve most common inaccuracy problems when it comes to DECIMAL columns

    0 讨论(0)
  • 2020-11-22 10:15

    Google provides a start to finish PHP/MySQL solution for an example "Store Locator" application with Google Maps. In this example, they store the lat/lng values as "Float" with a length of "10,6"

    http://code.google.com/apis/maps/articles/phpsqlsearch.html

    0 讨论(0)
  • 2020-11-22 10:16

    Basically it depends on the precision you need for your locations. Using DOUBLE you'll have a 3.5nm precision. DECIMAL(8,6)/(9,6) goes down to 16cm. FLOAT is 1.7m...

    This very interesting table has a more complete list: http://mysql.rjweb.org/doc.php/latlng :

    Datatype               Bytes            Resolution
    
    Deg*100 (SMALLINT)     4      1570 m    1.0 mi  Cities
    DECIMAL(4,2)/(5,2)     5      1570 m    1.0 mi  Cities
    SMALLINT scaled        4       682 m    0.4 mi  Cities
    Deg*10000 (MEDIUMINT)  6        16 m     52 ft  Houses/Businesses
    DECIMAL(6,4)/(7,4)     7        16 m     52 ft  Houses/Businesses
    MEDIUMINT scaled       6       2.7 m    8.8 ft
    FLOAT                  8       1.7 m    5.6 ft
    DECIMAL(8,6)/(9,6)     9        16cm    1/2 ft  Friends in a mall
    Deg*10000000 (INT)     8        16mm    5/8 in  Marbles
    DOUBLE                16       3.5nm     ...    Fleas on a dog
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 10:16

    The spatial functions in PostGIS are much more functional (i.e. not constrained to BBOX operations) than those in the MySQL spatial functions. Check it out: link text

    0 讨论(0)
  • 2020-11-22 10:21

    In a completely different and simpler perspective:

    • if you are relying on Google for showing your maps, markers, polygons, whatever, then let the calculations be done by Google!
    • you save resources on your server and you simply store the latitude and longitude together as a single string (VARCHAR), E.g.: "-0000.0000001,-0000.000000000000001" (35 length and if a number has more than 7 decimal digits then it gets rounded);
    • if Google returns more than 7 decimal digits per number, you can get that data stored in your string anyway, just in case you want to detect some flees or microbes in the future;
    • you can use their distance matrix or their geometry library for calculating distances or detecting points in certain areas with calls as simple as this: google.maps.geometry.poly.containsLocation(latLng, bermudaTrianglePolygon))
    • there are plenty of "server-side" APIs you can use (in Python, Ruby on Rails, PHP, CodeIgniter, Laravel, Yii, Zend Framework, etc.) that use Google Maps API.

    This way you don't need to worry about indexing numbers and all the other problems associated with data types that may screw up your coordinates.

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