# Calculate the distance between 2 points

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi! I've a table with a list of record that have latitude and
longitude

I've know new point with a position (new lati and new longi)

how can I show all records that have a distance <= of XX Kilometers??

Is it possible?

Thanks

## Re: Calculate the distance between 2 points

On 29/12/10 23:55, Sarah wrote:

You need a great circle calculator, and to calculate the great circle
distances between your new point and each point in the table.

Rgds

Denis McMahon

## Re: Calculate the distance between 2 points

Can you tell me how to calculate this using mySQL query and PHP?

Thanks

## Re: Calculate the distance between 2 points

Sarah wrote:

You're welcome.

PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

Sarah wrote:

Yes.

## Re: Calculate the distance between 2 points

I know this

\$distanceValue =
\$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

## Re: Calculate the distance between 2 points

Seems like your model needs to eat a lots of cookies or even cache some res=
ults :P

This will be a hudge amount of data so you better think about some algorith=
m to divide and conquer your problem. For example build a subset:

If you are in (Lat/Long) 35=B0 18' S 149=B0 7' E and want to find ONE neare=
st point then first take a look around and get everything that is 35 S 149 =
E. If there are more than get more detailled. Otherwise spread more. And so=
on.

This rectangle around your pos might speed up your algorithm, because only =
a small subset left over to deal with.

## Re: Calculate the distance between 2 points

Sarah wrote:

So RTFMySQLM.

PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.

## Re: Calculate the distance between 2 points

On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:

It seems to be the time of year, or possibly the lunar cycle, for
bullies to crawl out from under their rocks. Is this place the meeting
ground of choice for miserable people?

You can express an opinion in decent, respectful language. But crude and
insulting behavior gets no pass from me.

Dial it down, Pointed Ears.

Bill B

## Re: Calculate the distance between 2 points

On 12/29/2010 8:42 PM, Bill Braun wrote:

Bill, ignore "pointed head".  That's just how he is.  If he had to wait
until he had an intelligent answer, he wouldn't be able to post anything.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Calculate the distance between 2 points

Bill Braun wrote:

No. its the time of year when thick students who haven't done their
coursework expect someone else to do it for them.

No, its a place where people who should have listened to lectures, come

And students who cheat and crib get no pass from me either.

## Re: Calculate the distance between 2 points

On 12/30/2010 4:31 AM, The Natural Philosopher wrote:

Such points can be made, as forcefully as you wish, in a respectful
tone. There is no need or reason to belittle or demean.

Bill B

## Re: Calculate the distance between 2 points

Bill Braun wrote:

There is no reason not to, or to complain about it, either.

RTFM is a well known phrase.

FFS. ;-)

## Re: Calculate the distance between 2 points

On 12/30/2010 4:47 AM, The Natural Philosopher wrote:

Bullies, thugs, and tyrants count on reasonable people turning their
backs and letting their boorish behavior go unchecked. They are for the
most part cowards, who, when made accountable, tuck tail and run.

There is a reason, and I will complain, drawing attention to it as needed.

Bill B

## Re: Calculate the distance between 2 points

Could you please trim the messages you're drawing attention to?

Felix

## Re: Calculate the distance between 2 points

On 12/30/2010 1:32 PM, Felix Saphir wrote:

Of course. I thought I trimmed at the right spot, leaving some context.
Will redouble my efforts, thanks for the nudge.

Bill B

## Re: Calculate the distance between 2 points

On 12/30/2010 4:37 AM, Bill Braun wrote:

No, Bill, it is necessary for TNP to belittle and demean.  He needs to
feel superior to everyone else.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

## Re: Calculate the distance between 2 points

On 30/12/10 00:43, Sarah wrote:

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

## Re: Calculate the distance between 2 points

On 12/30/2010 7:15 AM, Denis McMahon wrote:

It may be possible to develop a non-linearity table with would produce a
correction factor for every X degrees of longitude, and store that in
the database as LONGADJ or the like. It would be as accurate as it is
detailed. A preliminary calculation would take the declining distance
per degree of longitude into account, and the result of that calculation
would be carried forward for the rest of the equation.

Bill B

## Re: Calculate the distance between 2 points

Bill Braun wrote:

Of you want to find the closest, and don't care about the exaxct
distance,  and exclude polar regions, you can simply find all longitude
offsets less than root 2 time s the smallest longitude offset, and do
the same with latitude, and that will give you a very small set and if
you have enough points, you can dis regard the sphericity and use plane
geometry.

should be a pretty fast 2 pas alogorithm through all the data with very
little maths to do.