Thursday, February 24, 2011

Android GPS How to calculate the distance between two GPS coordinates?

By Magesh Kumar   Posted at  3:56 AM   Android No comments


A simple function that takes two GPS coordinates as input and outputs the distance between them in meter. 
private double gps2m(float lat_a, float lng_a, float lat_b, float lng_b) {
  float pk = (float) (180/3.14169);

  float a1 = lat_a / pk;
  float a2 = lng_a / pk;
  float b1 = lat_b / pk;
  float b2 = lng_b / pk;

  float t1 = FloatMath.cos(a1)*FloatMath.cos(a2)*
     FloatMath.cos(b1)*FloatMath.cos(b2);
  float t2 = FloatMath.cos(a1)*FloatMath.sin(a2)*
     FloatMath.cos(b1)*FloatMath.sin(b2);
  float t3 = FloatMath.sin(a1)*FloatMath.sin(b1);
  double tt = Math.acos(t1 + t2 + t3);

  return 6366000*tt;
}

About the Author

Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9

0 comments:

Back to top ↑
Connect with Us

What they says

© 2013 MaGeSH 2 help. WP Mythemeshop Converted by BloggerTheme9
Blogger templates. Proudly Powered by Blogger.