Given two sets of lat,long how can we decide the distance

Tutorials that use the MapActivity. Many using GPS functionality.

Given two sets of lat,long how can we decide the distance

Postby laxmisaagar » Tue Oct 11, 2011 10:24 am

Hi,
I have two sets of long ,lat of points A and B . How do we decide the distance between these two points ?
Is there any android API to do that ? Or any hardcore logic ?

Thanks,
laxmisaagar :)
laxmisaagar
Freshman
Freshman
 
Posts: 6
Joined: Tue Oct 11, 2011 10:18 am

Top

Re: Given two sets of lat,long how can we decide the distanc

Postby lostdev » Tue Feb 21, 2012 6:27 pm

To the best of my knowledge there is no built in API for this.

You need to use the haversine formula for calculating great circle distances. I am not at a computer with my code so here's a javascript formula I found from googling. Adapt for whatever language you're coding in.

Syntax: [ Download ] [ Hide ]
Using javascript Syntax Highlighting
  1. var R = 6371; // km
  2. var dLat = (lat2-lat1).toRad();
  3. var dLon = (lon2-lon1).toRad();
  4. var lat1 = lat1.toRad();
  5. var lat2 = lat2.toRad();
  6.  
  7. var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
  8.         Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
  9. var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  10. var d = R * c;
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
lostdev
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Feb 21, 2012 6:22 pm

Top

Return to Map Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest