7

I am very new to Google maps I want calculate the distance between two places in android.

For that I get the two places lat and lag positions for that I write the following code:

private double getDistance(double lat1, double lat2, double lon1, double lon2) {
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double temp = 6371 * c;
    temp=temp*0.621;
    return temp;
}

The above code can't give the accurate distance between two places. What is the another way to find distance please give me any suggestions.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
user1787493
  • 179
  • 1
  • 4
  • 10

5 Answers5

20

Based on https://stackoverflow.com/a/13206484/, with one change

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);   // in meters
distance = locationA.distanceTo(locationB)/1000;   // in km
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Barış Çırıka
  • 1,570
  • 1
  • 15
  • 24
  • Its working fine but how can i check distance is right or wrong. – vijay Mar 04 '15 at 06:30
  • hey google does this calculation. who is more reliable then that. and you are providing the source and destination right. so if someone might go wrong here, that will be you. – Sagar Nayak Mar 10 '16 at 14:24
5

Using following this code you find distance but you want to convert in kilometer.

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);
Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39
ckpatel
  • 1,926
  • 4
  • 18
  • 34
  • based on lat and lan postions i want display the distance in km using google places api? – user1787493 Nov 03 '12 at 11:04
  • yes, using google places api u get distance but get in also meter so there also convert in kilometer. i already done in my project – ckpatel Nov 03 '12 at 11:06
  • using above mathod u get streate distance and google api get route path distance.eg. A to B distance is 500 meter so using api get 5oo meter but using above method get lessthan 5oo meter distance means get streate distance and method is good – ckpatel Nov 03 '12 at 11:10
  • suppose i am taking this lat and lang posions for hyd(17.38,780.24) chennai(13.06,80.24). i got 4772.0 will you tell me how do get correct distance? – user1787493 Nov 03 '12 at 11:26
  • getting streate path distance uding above mathod – ckpatel Nov 03 '12 at 11:28
  • Its working fine but how can i check distance is right or wrong. – vijay Mar 04 '15 at 06:30
1

Use code find distance meters to km and km to miles

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);   // in meters
distance = locationA.distanceTo(locationB)/1000;   // in km
distance = locationA.distanceTo(locationB)/1609.344;   // in miles
0

Just use Location.distanceTo(Location) it will give you a really distance between two different Locations.

Komal Nikhare
  • 252
  • 1
  • 4
  • 9
0
            Location locationA = new Location("Point A");
            locationA.setLatitude(fromLatitude);
            locationA.setLongitude(fromLongitude);


            Location locationB = new Location("point B");
            locationB.setLatitude(toLatitude);
            locationB.setLongitude(toLongitude);


            distance = locationA.distanceTo(locationB);
Raj Shah
  • 668
  • 1
  • 9
  • 23