How to get the direct distance between two places in android?

Read the Question carefully first ...

I need direct distance , not walking, car , etc.

Take a look at this image below.

Straight

Google provides us with car distance and driving.

But I do not want this, I want a direct distance between two locations (latitude - longitude).

What is displayed as RED LINE.

NOTE: I do not want to place the red line on the Google map, I just want the distance in units (mile, km, etc.)

+6
source share
3 answers

ANDROID

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

MATHEMATICALY

 a = distance in degrees //meterConversion = 1609; b = 90 - latitude of point 1 c = 90 - latitude of point 2 l = longitude of point 1 - longitude of point 2 Cos(a) = Cos(b)Cos(c) + Sin(b)Sin(c)Sin(l) d = circumference of Earth * a / 360 // circumference of Earth = 3958.7558657440545D km 
+15
source

The Haversine function is used to find the distance between two points on a sphere.

It is quite simple to extend this to the search for the distance between two points on Earth. Earth is not an ideal sphere, but it is still a good approximation using the standard measurement (WGS84) for a radius at the equator.

As CommonsWare said, you can do this very simply using distanceBetween (), which uses the Haversine function and the radius of WGS84.

For a better understanding of implementation / math, check out this sample code in Python.

+3
source

The distance you find using the following code. You just need to get two geographical latitudes and longitudes. and use this in the following calculation to get the distance.

  R = 6371; // km d = Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2) * Math.cos(lon2-lon1)) * R; 

This will be the reciprocal distance after all calculations.

R is the radius of the surface in KM, you must use it in the calculation, and you are trying to do it. Hope this is helpful for you.

+2
source

Source: https://habr.com/ru/post/949221/


All Articles