1

I have some confusion about calculation distance in miles. Whenever i am going to calculate in online it giving different result rather than i am using the below methods.

I need the distance in only miles. What the correction required in the below methods.

public double calculationByDistance(Location loc1, Location loc2) {
        int Radius = 6371;// radius of earth in Km
        double lat1 = loc1.getLatitude();
        double lat2 = loc2.getLatitude();
        double lon1 = loc1.getLongitude();
        double lon2 = loc2.getLongitude();
        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.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        km = km * 0.62137;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));
        Log.e("Radius Value", "" + valueResult + "   KM  " + kmInDec
                + " Meter   " + meterInDec);

        return Radius * c;
    }

If anyone have idea please reply.

Thanks in advance...

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Jagdish
  • 2,418
  • 4
  • 25
  • 51
  • 1
    Similar stackoverflow questions: http://stackoverflow.com/questions/120283/working-with-latitude-longitude-values-in-java http://stackoverflow.com/questions/837872/calculate-distance-in-meters-when-you-know-longitude-and-latitude-in-java – droidx Dec 22 '13 at 11:39
  • Thanks a lot @droidx. I will try the given link. – Jagdish Dec 22 '13 at 11:49

0 Answers0