5

I have 3 ways to calculate the distance and all 3 gives me different answers,

    double lat = 6.924049;
    double lng = 79.853807;

    double lat1 = 6.856461;
    double lng1 = 79.912748;

How to calculate the distance between two points and how to percent it on Km? or Meters?

1st way ans = 9.967441199417708E-6

float[] results = new float[1];
Location.distanceBetween(lat / 1E6, lng / 1E6, lat1 / 1E6, lng1 / 1E6,results);
float s =results[0] * 0.000621371192f;
String a2 = Float.toString(s);

2nd way ans = 6.1795527E6

double lat3 = lat / 1E6;
double lat2 = lat1 / 1E6;
double lon3 = lng / 1E6;
double lon2 = lng1 / 1E6;
double dLat = Math.toRadians(lat2 - lat3);
double dLon = Math.toRadians(lon2 - lon3);
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
double asd = c * 6378.1;

3rd way ans =6.176576174444191

{
double a5 = distance(lat, lng, lat1, lng1);
String a6 = Double.toString(a5);
}
private double distance(double lat1, double lon1, double lat2, double lon2) {
        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;
        return (dist);
    }

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

    private double rad2deg(double rad) {
        return (rad * 180.0 / Math.PI);
    }

So, How to calculate the distance between two points and how to percent it on KM ? or Meters ?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Loshi
  • 221
  • 1
  • 5
  • 19
  • How to calculate the distance between two points and how to percent it on Km? or Meters? – Loshi Jul 18 '12 at 04:35

5 Answers5

10

http://developer.android.com/reference/android/location/Location.html

Use the distanceTo or distanceBetween methods. Why dont you try the Android's Location method here

You can create a Location object from a latitude and longitude:

Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);
san
  • 1,845
  • 13
  • 23
  • 1
    Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid. – Lucifer Jul 18 '12 at 04:42
4

Try this formula

double dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1- y2, 2));

Or go to this link(More likely better for your case):

http://www.movable-type.co.uk/scripts/latlong.html

chandhooguy
  • 452
  • 3
  • 14
1

Google Maps Android API Utility Library calculates distance and more without trouble

You can use it with gradle


dependencies {
   compile 'com.google.maps.android:android-maps-utils:0.5+'
}

double distance = SphericalUtil.computeDistanceBetween(pointA, pointB);
noni_methadoni
  • 132
  • 1
  • 10
0

There is no way to find an accurate result while calculating distance of huge round place ( earth ). You will be having number of methods to find such distances. Now from your question, i would like to suggest you to use either 2nd or 3rd method because, from my point of view their result are somewhat equal ( possibly minor distance in few meters ).

For my personal development, I use your third method. And for precision i use 6 digits after the decimal point,

for e.g. in you case, 6.176576174444191 , i use 6.176576

Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • 6.176576174444191 answer is in what? meters? how can i make it a small figure? – Loshi Jul 18 '12 at 04:51
  • use rounding with decimal format, 6 digits after decimal point. – Lucifer Jul 18 '12 at 04:54
  • the problem is @Lucifer is that in google maps it gives a distance about 12km and here it gives only 6.18 meters? – Loshi Jul 18 '12 at 05:11
  • @Loshi, that sounds some mis-interpretation – Lucifer Jul 18 '12 at 05:19
  • yah that is my problem mate.... :( and distanceto and distance between gives another two different answers – Loshi Jul 18 '12 at 05:21
  • "There is no way to find an accurate result while calculating distance of huge round place ( earth )." Do you mean because the earth is not perfectly spherical so there will be variations? Because other than that, yes it is absolutely possible to accurately calculate the distance between two points on a sphere. It's just more complicated than a plane. – nasch Aug 17 '16 at 19:46
0
double ER=3958.75;
    double dlong=Math.toRadians(longitude1-longitude);
                                double dlat=Math.toRadians(latitude1-latitude);
                                a=(Math.sin(dlat/2)*Math.sin(dlat/2))+Math.cos(Math.toRadians(latitude))*Math.cos(Math.toRadians(latitude1))*Math.sin(dlong/2)*Math.sin(dlong/2);               
                                c=2*Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                                d=ER*c;

try with this it ll give the distance in kilometers or check with this link

Community
  • 1
  • 1
Nathiya
  • 88
  • 1
  • 8