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

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

    While it isn't optimal for all operations, if you are making map tiles or working with large numbers of markers (dots) with only one projection (e.g. Mercator, like Google Maps and many other slippy maps frameworks expect), I have found what I call "Vast Coordinate System" to be really, really handy. Basically, you store x and y pixel coordinates at some way-zoomed-in -- I use zoom level 23. This has several benefits:

    • You do the expensive lat/lng to mercator pixel transformation once instead of every time you handle the point
    • Getting the tile coordinate from a record given a zoom level takes one right shift.
    • Getting the pixel coordinate from a record takes one right shift and one bitwise AND.
    • The shifts are so lightweight that it is practical to do them in SQL, which means you can do a DISTINCT to return only one record per pixel location, which will cut down on the number records returned by the backend, which means less processing on the front end.

    I talked about all this in a recent blog post: http://blog.webfoot.com/2013/03/12/optimizing-map-tile-generation/

提交回复
热议问题