Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get straight distance between two location in android?

First read Question carefully ...

I need straight distance, not by walking,car,or etc.

Take a look to this image which given below,

Straight

Google provide us distance by car and driving.

But I don't want it, I want straight distance between two location (latitude - longitude).

Which is displayed as as RED LINE.

NOTE : I don't want to put red line on Google map, just want the Distance in Units(mile,km,etc.)

like image 938
Darshak Avatar asked Jul 11 '13 13:07

Darshak


People also ask

How do I draw a distance on Google Maps Android?

Open Google Maps and right-click on a starting point. On the menu that appears, click “Measure Distance.” 2. Click anywhere on the map to draw a line between the starting point and the destination point.


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
like image 141
Chirag Dj Avatar answered Nov 14 '22 23:11

Chirag Dj


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

It's fairly straightforward to extend this to finding the straight line distance between two points on the Earth. The Earth is not a perfect sphere, but this is still a good approximation using a standard measurement (called WGS84) for the radius at the equator.

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

For better understanding of implementation/math, take a look at this sample code in Python.

like image 26
msnider Avatar answered Nov 14 '22 22:11

msnider


Distance you find with following code. You just need to get two geoPoint's latitude and longitude. and use that in following calculation to get distance.

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

That will be return distance after all calculation.

R is the radius of surface in KM, need to use in calculation and you try this. I hope it is useful for you.

like image 39
Mr.Sandy Avatar answered Nov 14 '22 22:11

Mr.Sandy