How can I get the distance between my current location and a destination? I have the lat and long coordinates for the destination.
[import]uid: 3018 topic_id: 7479 reply_id: 307479[/import]
How can I get the distance between my current location and a destination? I have the lat and long coordinates for the destination.
[import]uid: 3018 topic_id: 7479 reply_id: 307479[/import]
The iOS sdk has a function CLLocation that gives you this info - is there a plan to let us access this?
In the meantime - I found the following article that describes how to calculate distance between two points on the earth:
http://www.movable-type.co.uk/scripts/latlong.html
The following code is based on this article and I’ve found it to be reasonably accurate - apparently to within .03% according to the link.
[code]
lat1 = 40.7143528; lon1 = -74.0059731 – New York, USA
lat2 = 48.8566667; lon2 = 2.3509871 – Paris, France
local earthRadius = 6371 – km
local dLat = math.rad(lat2-lat1)
local dLon = math.rad(lon2-lon1)
local a = math.sin(dLat/2) * math.sin(dLat/2) + math.cos(math.rad(lat1)) * math.cos(math.rad(lat2)) * math.sin(dLon/2) * math.sin(dLon/2);
local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a));
local d = earthRadius * c;
print(“distance”, d)
[/code] [import]uid: 3018 topic_id: 7479 reply_id: 26508[/import]
Dang, beat me to it. Was about to post the other distance function from that same page 
[code]
local gpsDistanceBetween = function ( pos1, pos2, radians )
local earthRadius = 6371
local lat1 = math.rad( pos1.latitude )
local lon1 = math.rad( pos1.longitude )
local lat2 = math.rad( pos2.latitude )
local lon2 = math.rad( pos2.longitude )
local distance = math.acos( math.sin ( lat1 ) * math.sin ( lat2 ) + math.cos( lat1 ) * math.cos ( lat2 ) * math.cos( lon2 - lon1 ) ) * earthRadius;
return distance
end
[/code] [import]uid: 5833 topic_id: 7479 reply_id: 26512[/import]