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

前端 未结 21 2414
梦如初夏
梦如初夏 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: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.

提交回复
热议问题