## Location.distanceBetween and distanceTo differences

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

### Location.distanceBetween and distanceTo differences

Hello everyone,

First of all, thank for all the help I have allready found on your forum, I am a 2month-beginner in Android dev' and your forum help me solved a lot of problems

Than, the purpose of my coming :
I encounter some issues on trying to calculate distance between one location and a list of others (in order to get the closest one). I tried by using Location.distanceBetween(...) and location.distanceTo(...), the two method are working but both of them send me different data and not any of them seems right.

My data came from a Class ConcessionDistance which is a basic extension of HashMap containing location data (as integer convert as string) and some other stuff that we don't need here.

Using java Syntax Highlighting
1. private ArrayList<ConcessionDistance> getMapsConcessionsDistancesFromPoint(ArrayList<ConcessionDistance> al, Location currentLocation) {
2.     Location dest;
3.     float distance1,distance2;
4.     float[] curDist = new float[1];
5.     double lati,longi;
6.     Log.i("DBTableConcessions","getMapsConcessionsDistancesFromPoint: Generation d'une map de donnée de concession");
7.     for(ConcessionDistance curConc : al){
8.         //Distance by method 1
9.         dest = new Location(LOCATION_PROVIDER);
10.         lati = new Double((new Integer(curConc.get("concession_latitude"))) /1e6);
11.         longi = new Double((new Integer(curConc.get("concession_longitude"))) /1e6);
12.         dest.setLatitude(lati);
13.         dest.setLongitude(longi);
14.         distance1 = currentLocation.distanceTo(dest);
15.         //Distance by method2
16.         Location.distanceBetween(
17.             currentLocation.getLatitude(),
18.             currentLocation.getLongitude(),
19.             (double) ((new Integer(curConc.get("concession_latitude"))) /1000000),
20.             (double) ((new Integer(curConc.get("concession_longitude"))) /1000000),
21.             curDist);
22.         distance2 = curDist[0];
23.         Log.i("DBTableConcessions","distance :"+distance1+" ou "+distance2);
24.         //Saving one of the distance
25.         curConc.put("distance",String.valueOf(distance1));
26.     }
27.     return al;
28. }
29.
30.
Parsed in 0.014 seconds, using GeSHi 1.0.8.4

Can some one help me, to figure out where is the problem? I think of a conversion problem but I'm to deep in the problem to solve it myself

Any help will be welcome. Thanks by advance.

Edit: Here is part of my LogCat: I send to the emulator the gps location of "Strasbourg" (those are cities from east-france) and process distance to all those cities:
Code: Select all
`06-29 06:37:37.215: INFO/DBTableConcessions(809): distance from EVRY :346254.66 ou 373061.3806-29 06:37:37.235: INFO/DBTableConcessions(809): distance from HAGUENAU CEDEX :105491.984 ou 0.006-29 06:37:37.235: INFO/DBTableConcessions(809): distance from BESANCON CEDEX :110127.08 ou 134302.0306-29 06:37:37.265: INFO/DBTableConcessions(809): distance from BREST CEDEX 9 :852829.1 ou 820181.706-29 06:37:37.295: INFO/DBTableConcessions(809): distance from HOENHEIM :87783.39 ou 0.006-29 06:37:37.326: INFO/DBTableConcessions(809): distance from COLMAR :28067.428 ou 0.006-29 06:37:37.355: INFO/DBTableConcessions(809): distance from BELFORT :40919.13 ou 134302.0306-29 06:37:37.446: INFO/DBTableConcessions(809): distance from SARREBOURG :81606.31 ou 0.0`

The real distance (given by google maps) between those cities and "Strasbourg" are :
Evry : 520km ( method1 : miss 175km, method2 : miss 145km)
Haguenau : 30km ( method1: add 70km, method2: return 0)
Besancon : 250km (method1: miss 140km, method2: miss 120km)
Brest : 1070km (method1: miss 200km, method2: miss 230km)
Hoenheim : 5km (method1: add 80km, method2: return 0)
Colmar : 75km (method1: miss 50km, method2: return 0)
Belfort : 155km (method1: miss 110km, method2: miss 20km)
Sarrebourg : 75km (method1: miss 5km, method2: return 0)

So we can notice that the second method return 0 when real distance is below ~100km, wherease for the method1 I can't see any logic inhere :/
Twano_O
Freshman

Posts: 3
Joined: Tue Jun 28, 2011 5:52 pm

### Re: Location.distanceBetween and distanceTo differences

I have added a third calculation process, with this :

Code: Select all
`public class DeltaCoord {   static double CoordToDeltaKm(        double Q1Latitude,double Q1LatiDeg, char Q1LatiDirection,        double Q1Longitude,double Q1LongDeg, char Q1LongDirection,        double Q2Latitude,double Q2LatiDeg, char Q2LatiDirection,        double Q2Longitude,double Q2LongDeg, char Q2LongDirection    ) {       double a1,b1,a2,b2,RawDelta;       a1=(Q1Latitude+(Q1LatiDeg/60))*Math.PI/180;       if(Q1LatiDirection=='N') a1=-a1;       b1=(Q1Longitude+(Q1LongDeg/60))*Math.PI/180;       if(Q1LongDirection=='O') b1=-b1;       a2=(Q2Latitude+(Q2LatiDeg/60))*Math.PI/180;       if(Q2LatiDirection=='N') a2=-a2;       b2=(Q2Longitude+(Q2LongDeg/60))*Math.PI/180;       if(Q2LongDirection=='O') b2=-b2;       RawDelta = Math.acos(Math.cos(a1)*Math.cos(b1)*Math.cos(a2)*Math.cos(b2) + Math.cos(a1)*Math.sin(b1)*Math.cos(a2)*Math.sin(b2) + Math.sin(a1)*Math.sin(a2));       return RawDelta * 6378.0;    }      public static double distance(Location a, Location b){      return CoordToDeltaKm(a.getLatitude(),0,' ',a.getLongitude(),0,' ',b.getLatitude(),0,' ',b.getLongitude(),0,' ');   }}`