FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Calculate the distance between 2 points
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Calculate the distance between 2 points [message #171327 is a reply to message #171279] Thu, 30 December 2010 12:15 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On 30/12/10 00:43, Sarah wrote:
> I know this
>
> $distanceValue =
> rad2deg(acos(sin(deg2rad($LATITUDINE1))*sin(deg2rad($LATITUDINE2))
> +cos(deg2rad($LATITUDINE1))*cos(deg2rad($LATITUDINE2))*cos(deg2rad($LONGITU DINE1-
> $LONGITUDINE2))))*69.09;
>
> or something like this.... but I don't want to have all records of DB
> and than analize them one by one .... I want a query the return only
> nearest values

Then you have to do the calculation as part of the query.

What you can do is pre-calculate some latitude and longitude limits, for
example 1 degree of latitude always equates to the same distance north
to south.

Polar circumference is 40,008 km, half that is 20,004 km, divide by 180
degrees, and you get 111.13333 km per degree of latitude. So, to make it
easy, for every 100 km of distance that you want to limit to, you only
need to check +- 1 degree of latitude of your given point (a more
precise figure is 0.9 degrees).

Therefore, if you're checking for places within 300km, you can
immediately dismiss anywhere that's more than 3 (or 2.7) degrees of
latitude away from your specified location.

Longitude is trickier, because the distance per degree of longitude
varies from 111.3222222 miles at the equator to 0 miles at the pole.

However, you can try and calculate a "box", outside which you don't need
to calculate the actual great circle distance because it will always be
greater than the range you are specifying. You will need to be careful
if your box embraces either a pole or the equator that you don't dismiss
valid points.

What you could then do is copy all the points inside your "might fit"
box into a temporary table, and then run a query on the temporary table
to determine their actual distances. This might be less computationally
intensive than calculating the great circle range for every point.

Rgds

Denis McMahon
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Checking equal number of <div> and </div>
Next Topic: extract variable from an array
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 07:21:51 GMT 2024

Total time taken to generate the page: 0.03760 seconds