Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the distance between two geopoints?

double distance;  

Location locationA = new Location("point A");  

locationA.setLatitude(latA);  
locationA.setLongitude(lngA);  

Location locationB = new Location("point B");  

locationB.setLatitude(latB);  
LocationB.setLongitude(lngB);  

distance = locationA.distanceTo(locationB);  

the above code is not working and i am getting 0.0 Km as distance? Also in the constructor of the location class, what does the string provider mean. In the above code i am using PointA and PointB as the providers.

why is the above code not working?

thank you in advance.

LOGCAT 05-09 17:48:56.144: INFO/current loc(1573): lat 0.0 lng 0.0 05-09 17:48:56.155: INFO/checklocation loc(1573): lat 54.4288665 lng 10.169366

like image 670
user590849 Avatar asked May 09 '11 12:05

user590849


People also ask

How do you find the distance between 2 points?

Distance between two points is the length of the line segment that connects the two points in a plane. 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.

How do you find the distance between two diagonals?

This means that you will square the x-axis distance (x2 - x1), and that you will separately square the y-axis distance (y2 - y1). Add the squared values together. This will give you the square of the diagonal, linear distance between your two points.


1 Answers

Just a quick snippet since I didn't see a complete and simple solution with GeoPoints above:

public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
    double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
    double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
    double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
    double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
    float [] dist = new float[1];
    Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
    return dist[0] * 0.000621371192f;
}

If you want meters, just return dist[0] directly.

like image 144
Glen Avatar answered Nov 02 '22 23:11

Glen