3

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.

private ArrayList<ConcessionDistance> getMapsConcessionsDistancesFromPoint(ArrayList<ConcessionDistance> al, Location currentLocation) {
    Location dest;
    float distance1,distance2;
    float[] curDist = new float[1];
    double lati,longi;
    Log.i("DBTableConcessions","getMapsConcessionsDistancesFromPoint: Generation d'une map de donnée de concession");
    for(ConcessionDistance curConc : al){
        //Distance by method 1
        dest = new Location(LOCATION_PROVIDER);
        lati = new Double((new Integer(curConc.get("concession_latitude"))) /1e6);
        longi = new Double((new Integer(curConc.get("concession_longitude"))) /1e6);
        dest.setLatitude(lati);
        dest.setLongitude(longi);
        distance1 = currentLocation.distanceTo(dest);
        //Distance by method2
        Location.distanceBetween(
            currentLocation.getLatitude(), 
            currentLocation.getLongitude(), 
            (double) ((new Integer(curConc.get("concession_latitude"))) /1000000), 
            (double) ((new Integer(curConc.get("concession_longitude"))) /1000000),
            curDist);
        distance2 = curDist[0];
        Log.i("DBTableConcessions","distance :"+distance1+" ou "+distance2);//+" ,lat :"+lati+" ,long "+longi);  //+"curDist{0="+curDist[0]+",1="+curDist[1]+",2="+curDist[2]+"}"
        //Saving one of the distance
        curConc.put("distance",String.valueOf(distance1));
    }
    return al;
}

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:

06-29 06:37:37.215: INFO/DBTableConcessions(809): distance from EVRY :346254.66 ou 373061.38
06-29 06:37:37.235: INFO/DBTableConcessions(809): distance from HAGUENAU CEDEX :105491.984 ou 0.0
06-29 06:37:37.235: INFO/DBTableConcessions(809): distance from BESANCON CEDEX :110127.08 ou 134302.03
06-29 06:37:37.265: INFO/DBTableConcessions(809): distance from BREST CEDEX 9 :852829.1 ou 820181.7
06-29 06:37:37.295: INFO/DBTableConcessions(809): distance from HOENHEIM :87783.39 ou 0.0
06-29 06:37:37.326: INFO/DBTableConcessions(809): distance from COLMAR :28067.428 ou 0.0
06-29 06:37:37.355: INFO/DBTableConcessions(809): distance from BELFORT :40919.13 ou 134302.03
06-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 :/

Weber Antoine
  • 191
  • 2
  • 2
  • 10

1 Answers1

1

Ok, so correction for this problem is : first I needed to enter correct value in my lati,longi. This can be done with:

 lati = Double.valueOf(curConc.get("concession_latitude"))/1000000;

this way, all method give me the same distances, so the calculation method works fine.

afterwards, the Location given in parameters wasn't correct neither. In fact they don't contains fractional data. So I know were is the problem but I do not know how to resolv it. In fact the line who gave me bad data are here

final LocationManager manag = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            //Choix du service de localisation en fonction de critères
            Criteria crit = new Criteria();
            crit.setCostAllowed(false);
            crit.setAccuracy(100); //précis à 1km pret
            String choix_source = manag.getBestProvider(crit, true);
            //demander la position courante
            manag.requestLocationUpdates(choix_source, 10000, 0, new LocationListener(){ //toutes les 10 secondes
                //Lors de la capture de la position
                public void onLocationChanged(Location location) {
                ....
Weber Antoine
  • 191
  • 2
  • 2
  • 10