Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating distance between two geographic locations

please shed some light on this situation

Right now i have two array having latitude and longitude of nearby places and also have the user location latiude and longiude now i want to calculate the distance between user location and nearby places and want to show them in listview.

I know that there is a method for calculating distance as

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results); 

Now what is the problem is how to pass these two array having nearby latitude and longitue in this method and get the array of distances.

like image 890
Sunny Avatar asked Nov 08 '11 11:11

Sunny


People also ask

How do you find the distance between two locations using latitude and longitude?

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...

What is the distance between 2 latitudes?

What is the Distance Between Lines of Latitude? Lines of latitude are called parallels and in total there are 180 degrees of latitude. The distance between each degree of latitude is about 69 miles (110 kilometers).


1 Answers

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

Look into distanceTo

Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid.

or distanceBetween

Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.

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

Location locationA = new Location("point A");  locationA.setLatitude(latA); locationA.setLongitude(lngA);  Location locationB = new Location("point B");  locationB.setLatitude(latB); locationB.setLongitude(lngB);  float distance = locationA.distanceTo(locationB); 

or

private double meterDistanceBetweenPoints(float lat_a, float lng_a, float lat_b, float lng_b) {     float pk = (float) (180.f/Math.PI);      float a1 = lat_a / pk;     float a2 = lng_a / pk;     float b1 = lat_b / pk;     float b2 = lng_b / pk;      double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);     double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);     double t3 = Math.sin(a1) * Math.sin(b1);     double tt = Math.acos(t1 + t2 + t3);         return 6366000 * tt; } 
like image 195
Karthi Avatar answered Oct 06 '22 12:10

Karthi