Thursday 14 March 2013

Android GPS Method for Calculate the distance between two GPS coordinates..


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;
}

2 comments:

  1. can u kindly upload tutorial on a routing algorithm like A* for calculating shortest path.

    ReplyDelete
  2. Hi Mehwish,
    you can use OpenTripPlanner for Android. it is open source and easy to use
    https://github.com/CUTR-at-USF/OpenTripPlanner-for-Android/wiki

    ReplyDelete