0

I'm computing a distance between user's current location and a given coordinate. But I am getting different values for my 2 test devices (Galaxy S3 and Galaxy S4).

public static double getDistance(double lat1, double lon1, double lat2,double lon2,String unit) {
    double theta = lon1 - lon2;
    double dist = (Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))) * Math.cos(deg2rad(theta));

    dist = Math.acos(dist):
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    if (unit == 'K'){
        dist = dist * 1.609344 * 1000;
    }else if (unit=='N') {
        dist = dist * 0.8684;
    }

    DecimalFormat df = new DecimalFormat("#.0");

    double metres = (Double.valueOf(df.format(dist)));

    return metres;
}

private static double deg2rad(double deg) {
    return (deg*Math.PI / 180.0);
}

private static double rad2deg(double rad){
    return (rad*180.0 / Math.PI);
}
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • possible duplicate of [Android distance between two points](http://stackoverflow.com/questions/11534323/android-distance-between-two-points) – Maveňツ Sep 03 '14 at 06:12
  • Could you explain more what do you mean by *getting different values for my 2 test devices (Galaxy S3 and Galaxy S4)*? An example would be useful. – Andrew T. Sep 03 '14 at 06:14
  • Did you get the same latlng of your current location on both S3 and S4 if not so your device may be returning you the last known location that it was saved before. – Hasnain Sep 03 '14 at 06:41
  • I'm reading the same file for the latlng. – user2385014 Sep 03 '14 at 07:17
  • Ex. My current location is lat 1.3316535 lon 103.8487563. Im computing the distance bet current location to lat 1.3367406266337 lon 103.8505371999740. In my s3 device it displays 600.5m and in my s4 it displays 1.154667E7m – user2385014 Sep 03 '14 at 07:23

0 Answers0