Calculation of the distance between two geographical points

I ask you to shed light on this situation.

Right now I have two arrays having the latitude and longitude of neighboring places, and also now the location of the latiude and longiude user now I want to calculate the distance between the user's location and neighboring places and want to show them in the list.

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 the problem is how to pass these two arrays having close latitude and length in this method and get an array of distances.

+41
android latitude-longitude location distance
Nov 08 2018-11-11T00:
source share
6 answers

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

Look at distanceTo or distanceBetween. You can create a Location object from 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; } 
+130
Nov 08 '11 at 12:20
source share

Try this code. here we have two values โ€‹โ€‹of longitude and latitude, and the selected_location.distanceTo (near_locations) function returns the distance between these places in meters.

 Location location1=new Location("locationA"); near_locations.setLatitude(17.372102); near_locations.setLongitude(78.484196); Location location2=new Location("locationA"); near_locations.setLatitude(17.375775); near_locations.setLongitude(78.469218); double distance=selected_location.distanceTo(near_locations); 

here "distance" is the distance between location1 and place2 (in meters)

+8
Oct 10 '13 at 14:04 on
source share

There is only one user location, so you can iterate over the list of nearby places, you can call the distanceTo() function to get the distance, you can save it in an array if you want.

From what I understand, distanceBetween() for distant places, its way out is the WGS84 ellipsoid.

+3
Nov 08 '11 at 11:50
source share
  private static Double _MilesToKilometers = 1.609344; private static Double _MilesToNautical = 0.8684; /// <summary> /// Calculates the distance between two points of latitude and longitude. /// Great Link - http://www.movable-type.co.uk/scripts/latlong.html /// </summary> /// <param name="coordinate1">First coordinate.</param> /// <param name="coordinate2">Second coordinate.</param> /// <param name="unitsOfLength">Sets the return value unit of length.</param> public static Double Distance(Coordinate coordinate1, Coordinate coordinate2, UnitsOfLength unitsOfLength) { double theta = coordinate1.getLongitude() - coordinate2.getLongitude(); double distance = Math.sin(ToRadian(coordinate1.getLatitude())) * Math.sin(ToRadian(coordinate2.getLatitude())) + Math.cos(ToRadian(coordinate1.getLatitude())) * Math.cos(ToRadian(coordinate2.getLatitude())) * Math.cos(ToRadian(theta)); distance = Math.acos(distance); distance = ToDegree(distance); distance = distance * 60 * 1.1515; if (unitsOfLength == UnitsOfLength.Kilometer) distance = distance * _MilesToKilometers; else if (unitsOfLength == UnitsOfLength.NauticalMiles) distance = distance * _MilesToNautical; return (distance); } 
+3
Oct 11 '13 at 14:11
source share

distanceTo will give you the distance in meters between the two given location ej target.distanceTo (destination).

distanceBetween also gives the distance, but it will save the distance in the float array (results [0]). the document says that if the results are 2 or more in length, the original bearing is stored in the results [1]. If the results have a length of 3 or more, the final bearing is stored in the results [2]

hope this helps

I used distanceTo to get the distance from point A to B, I think this is the way to go.

0
Sep 05 '13 at 14:40
source share
 public double distance(Double latitude, Double longitude, double e, double f) { double d2r = Math.PI / 180; double dlong = (longitude - f) * d2r; double dlat = (latitude - e) * d2r; double a = Math.pow(Math.sin(dlat / 2.0), 2) + Math.cos(e * d2r) * Math.cos(latitude * d2r) * Math.pow(Math.sin(dlong / 2.0), 2) double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); double d = 6367 * c; return d; } 
0
Apr 19 '16 at 17:35
source share



All Articles