Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the distance between two point by latlng?

Tags:

I want to get the distance from A to B by latlng.When i run my app with the following code,it return a wrong result. Fx i tryed A( 31.172740,115.0081630) ,then B(30.6055980,114.3603140),the result is about 3111km.but the right distance is about 134km. My code is:

    class btnListener implements OnClickListener{

    public void onClick(View v) {
         lat_a=lat_A.getText().toString();
         lng_a=lng_A.getText().toString();
         lat_b=lat_B.getText().toString();
         lng_b=lng_B.getText().toString();
         double a1,n1,a2,n2;
         a1=30.6055980;
         a2=Double.parseDouble(lat_b);
         n2=Double.parseDouble(lng_b);
         double R=6371;
         double D=Math.acos(Math.sin(a1)*Math.sin(a2)+Math.cos(a1)*Math.cos(a2)*Math.cos(n2-n1));
         Toast.makeText(getApplication(), "the distance is"+String.valueOf(D*R)+"km from A to B", Toast.LENGTH_SHORT).show();
    }

}
like image 929
lanyimo Avatar asked Jan 12 '12 08:01

lanyimo


People also ask

How do you find the distance between two Latlng?

from math import cos, asin, sqrt, pi def distance(lat1, lon1, lat2, lon2): p = pi/180 a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2 return 12742 * asin(sqrt(a)) #2*R*asin... And for the sake of completeness: Haversine on Wikipedia.

How do you find a distance between two points?

The formula to find the distance between the two points is usually given by d=√((x2 – x1)² + (y2 – y1)²). This formula is used to find the distance between any two points on a coordinate plane or x-y plane.

What is the distance between 2 longitude?

One-degree of longitude equals 288,200 feet (54.6 miles), one minute equals 4,800 feet (0.91 mile), and one second equals 80 feet.


4 Answers

There is an api provided by android itself to get distance between two points. Use:

Location.distanceBetween(
    startLatitude,
    startLongitude,
    endLatitude,
    endLongitude,
    results);

which returns distance in meters.

like image 148
jeet Avatar answered Oct 02 '22 17:10

jeet


Try below code.

public float distance (float lat_a, float lng_a, float lat_b, float lng_b ) 
{
    double earthRadius = 3958.75;
    double latDiff = Math.toRadians(lat_b-lat_a);
    double lngDiff = Math.toRadians(lng_b-lng_a);
    double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +
    Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
    Math.sin(lngDiff /2) * Math.sin(lngDiff /2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double distance = earthRadius * c;

    int meterConversion = 1609;

    return new Float(distance * meterConversion).floatValue();
}

If you didn't understood, please check this link, same code available here.

like image 21
Yugandhar Babu Avatar answered Oct 02 '22 15:10

Yugandhar Babu


LatLng startLatLng = new LatLng(startLatitude, startLongitude);
LatLng endLatLng = new LatLng(endLatitude, endLongitude)
double distance = SphericalUtil.computeDistanceBetween(startLatLng, endLatLng);

also add

compile 'com.google.android.gms:play-services-maps:15.0.1'
compile 'com.google.maps.android:android-maps-utils:0.5+'

to your gradle dependencies

Distance in meters

like image 23
dilettante Avatar answered Oct 02 '22 16:10

dilettante


Use the google distance Api to get the actual distance

https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=lat,lng&destinations=lat,lng&key={your google map key}&mode=driving

It will return u the distance in km.

like image 31
Anil Dhiman Avatar answered Oct 02 '22 15:10

Anil Dhiman