Calculating distances...

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Calculating distances...

Post by paulsr » Tue Oct 16, 2012 11:36 am

Hi,

Does anyone know of a routine that will calculate the distance (in meters, or whatever) between two sets of co-ordinates that have been returned by iphoneCurrentLocation() calls?

Many tkx...

--paul

combleurdelacunes
Posts: 2
Joined: Mon Apr 10, 2006 8:43 pm

Re: Calculating distances...

Post by combleurdelacunes » Tue Oct 16, 2012 2:53 pm

I don't know what values you have for your locations, but I have a formula.

There are limits : I need Longitude/Latitude OR Radian values
Calculations are good to a kilometre or so,
certainly not down to metres

Best Regards

-Francis
"Nothing should ever be done for the first time !"

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Calculating distances...

Post by sturgis » Tue Oct 16, 2012 3:03 pm

I think current location returns a signed real so you won't have to convert. If that is the case then the following function should be useful.

Code: Select all

## pR is radius, the lats and lons are decimal reperesntaions of the start and end point
function calcDist pR,plat1,plon1,plat2,plon2
   ## convert to radians
   put plat1 / 180 * pi into lat1
   put plon1 / 180 * pi into lon1
   put plat2 / 180 * pi into lat2
   put plon2 / 180 * pi into lon2
   
   put lat2 - lat1 into dlat
   put lon2 - lon1 into dlon
   
   put (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 into a
   put 2 * atan2( sqrt(a),sqrt(1-a)) into c
   
   return pR * c
end calcDist
An approximate value for pR would be 3961 for miles & 6373 for km

All this info was found at http://andrew.hedges.name/experiments/h ... 34&mi=&km= If you need to convert between decimal form and degrees minutes seconds you can find info on that at http://andrew.hedges.name/experiments/convert_lat_long/

My numbers and their numbers come out slightly different using the same calc, but this is probably due to handling rounding/trunction differently. (looked at the underlying code, looks like some things are limited to 2 decimal places whcih I did not do)

As Francis said, (and as the page mentions due to varying radius measurements) accuracy isn't all that good but should be close enough hopefully.

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: Calculating distances...

Post by paulsr » Wed Oct 17, 2012 3:53 am

That's great. Thanks guys.

--paul

combleurdelacunes
Posts: 2
Joined: Mon Apr 10, 2006 8:43 pm

Re: Calculating distances...

Post by combleurdelacunes » Wed Oct 17, 2012 12:27 pm

And just in case you want the liveCode script. Nothing special, but saves you coding it !! :

put RFHaversine (LVRadLat1,LVRadLong1,LVRadLat2,LVRadLong2) into field Distance

Function RFHaversine PVLat1,PVLong1,PVLat2,PVLong2
--
--global LVRadiusOfTheEarth,LVLat1,LVLong1,LVLat2,LVLong2,LVInt1,LVInt2,LVWork1,LVWork2,LVWork3
--
-- Use Haversine two-argument Inverse Tangent Function to calculate
-- the distance between two geographic locations (cities).
--
-- Input Parameters :
--
-- Latitude and Longitude of location 1 in Radians (PVLat1,PVLong1).
-- Latitude and Longitude of location 2 in Radians (PVLat2,PVLong2).
--
set numberFormat to 0.000000000000 -- Heavy precision required here !
put 6367 into LVRadiusOfTheEarth -- Generally accepted Radius of the Earth
put PVLat1 into LVLat1
put PVLong1 into LVLong1
put PVLat2 into LVLat2
put PVLong2 into LVLong2
put LVLong2 - LVLong1 into LVInt1
put LVLat2 - LVLat1 into LVInt2
put (sin(LVInt2/2))^2 + cos(LVLat1) * cos(LVLat2) * (sin(LVInt1/2))^2 into LVWork1
put 2 * atan2(sqrt(LVWork1),sqrt(1-LVWork1)) into LVWork2
put LVRadiusOfTheEarth * LVWork2 into LVWork3
--
-- We now have the distance in kilometres in LVWork3.
--
put round(LVWork3,2) into LVWork3
return LVWork3
end RFHaversine

Best Regards

-Francis
"Nothing should ever be done for the first time !"

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Calculating distances...

Post by Dixie » Wed Oct 17, 2012 1:48 pm

Are you wanting to calculate the distances between the two points 'as the crow flies' or along a road or a path that connects the two points ?
be well

Dixie

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Calculating distances...

Post by sturgis » Wed Oct 17, 2012 5:28 pm

The 2 functions posted are the same method, the only difference being that mine takes degrees and converts to radians and allows you to choose radius numbers, the other takes arguments as radians directly and hard codes kilometers.

Also Craig asks a good question. If you aren't looking for distance "as the crow flies" and want driving distance you could use the google maps api to handle that, or possibly mergExt externals package for its mapping stuff. Not sure exactly what capabilities the external has though so you'd have to check into it and see if it will give driving distances.

paulsr
Posts: 222
Joined: Thu Jul 19, 2012 9:47 am
Contact:

Re: Calculating distances...

Post by paulsr » Thu Oct 18, 2012 4:13 am

Thanks for the comments, but I think either of the above pieces of code will work fine.

Yes, I want driving distances, but ones that will be updated constantly. And the app will be used mostly off-road, using "roads" that Google Maps won't show.

--paul

Post Reply