MySQL select coordinates within range

后端 未结 5 1682
有刺的猬
有刺的猬 2020-11-28 12:38

I\'ve in my database 100 000 addresses (that is records).

Each one of them has its own coordinates (latitude and longitude).

Now, given the geo location of

5条回答
  •  悲&欢浪女
    2020-11-28 12:49

    My approach - and I am using it - is to think like a technician, I am happy with +/- 5%

    This solution is not meant to control rockets, boats or a like, and only for distances below ~100km its just a solution like Fermi problem

    So lets start for a pragmatic solution:

    First: for many problems we can ignore that earth is not flat (distance <~100km) ,

    The circumference of the earth is more or less 40000 km (more or less exactly for some reason)

    A circle has exactly 360 deg.

    So 1 km is: 360/40000 deg => 0.00278 deg

    And then just select lat/long within this 0.003 per km, it will be something like where long > 42 - 0.003 and long < 42 + 0.003 - same for lat, where 42 is your lat/long as middpoint. The Database will use the indices.

    the problem: you get a square as result not a circle (not real distance)

    If you need the circle, script it after getting the results.

    I just show the official toilette with in 2 km, so a square is ok, and the accuracy, too. The reason is, that there are streets and houses, so people can't walk directly ...

    Edit: Technical / mathematical explain:

    For very small triangles (one angle << 5 deg) you can ignore the use of trigonometry. So sin(5 deg) ~= 5/180*PI

提交回复
热议问题