Skip to content Skip to sidebar Skip to footer

Android Distance Between Two Points

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 =

Solution 1:

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:

Locationlocation=newLocation("");
location.setLatitude(lat);
location.setLongitude(lon);

Solution 2:

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

Solution 3:

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

Solution 4:

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

Solution 5:

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

Post a Comment for "Android Distance Between Two Points"