Get results that fall within marker radiuses from database

前端 未结 3 736
温柔的废话
温柔的废话 2020-11-29 03:51

Update 16th November 2012

I would like to raise this question again, offering with a new bounty for a solid, good solution. It seems that only the solution (shubha

3条回答
  •  再見小時候
    2020-11-29 04:22

    At the heart of your problem is the question "how do I know if two circles overlap". The answer to that is "if the distance between their centers is less than the sum of their radii". So what you're looking for is how to determine the distance between two points.

    The other answer is treating latitude and longitude as if they comprise a Cartesian plane. which they do not (longitude tends towards zero as you approach the poles from the equator). Now, as an approximation, it may work just fine for your solution, depending on the accuracy needed by your solution. On the other hand, if you need this to be very accurate, you need the Haversine formula. There's a great description of how to implement it in MySQL here:

    http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

    From slide 7 of that presentation, you have the following formula:

    3956*2*ASIN(SQRT(POWER(SIN((orig.lat-dest.lat)*pi()/180/2),2)+
        COS(orig.lat*pi()/180)*COS(dest.lat*pi()/180)*
        POWER(SIN((orig.lon-dest.lon)*pi()/180/2),2)))
    

    Note that the first number is the mean radius of the earth in miles; change that to 6371 for kilometers.

    How you use this calculated distance is going to depend on details not included in your post, such as the number of points you're dealing with, the geographic dispersion, any performance requirements, and whether or not the data is static or being continually updated.

    I mention these things because performance is going to be an issue, especially if you have any significant amount of data and/or it's being continuously updated (like user's locations based on their phone's GPS data).

    One way you can help with the performance issue is use squares instead of circles, and use the approximation of one degree = 111.12 km. That way you can automatically cull out any points that are obviously far away from one another. Then you're left just calculating the Haversine formula only for the handful of points that fall within the area of interest.

    I hope this is helpful in pointing you in the right direction.

提交回复
热议问题