Android distance between two points

I have 3 ways to calculate distance, and all 3 give 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 as a percentage value on Km? or meters?

1st method 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);

The second method 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;

The third method 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 is the percentage value on KM? or meters?

+5
source share
5 answers

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

Use the distanceTo or distanceBetween methods. Why aren't you trying to use the Android location method here

Location :

Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);
+10

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

( , ):

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

+1

Google API Android API-

gradle


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

double distance = SphericalUtil.computeDistanceBetween(pointA, pointB);
+1

(). . 2-, 3- , , , (, ).

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

eg. in your case, 6.176576174444191, I am using 6.176576

0
source
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 to give a distance in kilometers with this or check with this link

0
source

All Articles