Thursday, March 8, 2012

Android Calculate distance between two geo location In KM

Android: calculate distance between two geo location in KM.

Below i added code for distance between two geo location in KM

Just you have to pass Start lat , lng and Destination lat ,lag value.

Screen Shot

Source Code :

    public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
       
        double lat1 = StartP.getLatitudeE6()/1E6;
  
        double lat2 = EndP.getLatitudeE6()/1E6;
  
        double lon1 = StartP.getLongitudeE6()/1E6;
  
        double lon2 = EndP.getLongitudeE6()/1E6;
  
        double dLat = Math.toRadians(lat2-lat1);
  
        double dLon = Math.toRadians(lon2-lon1);
  
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
  
        Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
  
        Math.sin(dLon/2) * Math.sin(dLon/2);
  
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

//6378.1 is gravity of earth
        return  c*6378.1;
  
     }


You can Download SOURCE CODE HERE

13 comments:

  1. hi, nice post...can you mail me this source code. i have a problem in downloading source code.
    rp_167@yahoo.co.in

    ReplyDelete
  2. mail me tooo....shubhabhagya@gmail.com
    thanks for advance

    ReplyDelete
  3. problem with downloading source code. can u send to my mail:aspkumar444@gmail.com

    ReplyDelete
  4. problem in downloading the source code.please help us.

    ReplyDelete
  5. downloading prob dude!!! mail plz dreamjai007@gmail.com

    ReplyDelete
  6. I am not able to download the source code, it is taking me to fill some survey.

    ReplyDelete
  7. Hello Sir,
    Thanks for your tutorials. Please I want to ask if it can be possible for you to show me the source code for Calculating the distance between two locations. I will appreciate your reply.

    Thanks.
    Modestus

    ReplyDelete
  8. Its goin to somother page

    ReplyDelete
  9. hey hi....can any one please help me in this case:i have 2 images (both are draggable on the screen) when one touch each other it has to stop moving...

    ReplyDelete

Check out this may be help you

Related Posts Plugin for WordPress, Blogger...