0

I am trying to calculate distance between a location(source location marker) to the other locations stored in an ArrayList(returned from RoutesData) and storing the locations along the distances in another ArrayList and then selecting the LatLng with minimum distance .My code for the particular logic is.

Class RoutesData : returns the LatLngs to measure distance

public class RoutesData {

    public ArrayList<LatLng> allRoutesMainStops(){
        ArrayList<LatLng> allPoints = new ArrayList<LatLng>(Arrays.asList(
                //icPoints
                new LatLng(33.582752, 73.044503),new LatLng(33.595504, 73.050912),
                //station
                new LatLng(33.601097, 73.047798),new LatLng(33.598755, 73.055607),new LatLng(33.599970, 73.063225),new LatLng(33.602757, 73.066996),new LatLng(33.604297, 73.075843),new LatLng(33.608692, 73.082024),
                //ali nawaz
                new LatLng(33.617330, 73.081743),
                //centre hosp
                new LatLng(33.630072, 73.071996),new LatLng(33.631454, 73.072416),new LatLng(33.633905, 73.062379),new LatLng(33.641462, 73.063376),new LatLng(33.646714, 73.064095),new LatLng(33.651782, 73.064535),new LatLng(33.661329, 73.063974),new LatLng(33.672490, 73.055906),new LatLng(33.683642, 73.047215),new LatLng(33.689706, 73.030363),new LatLng(33.680982, 73.018654),new LatLng(33.671293, 73.016894),
                //gpo 1
                new LatLng(33.595215, 73.051496),new LatLng(33.593119, 73.054184),new LatLng(33.585280, 73.066763),new LatLng(33.588925, 73.076172),new LatLng(33.599117, 73.080001),new LatLng(33.607101, 73.083641),new LatLng(33.626583, 73.075027),
                //new LatLng(33.630072, 73.071996), //duplicate center
                new LatLng(33.631550, 73.072534),new LatLng(33.639063, 73.075742),new LatLng(33.643424, 73.077372),new LatLng(33.650480, 73.080152),new LatLng(33.663188, 73.085446),new LatLng(33.696970, 73.062966),new LatLng(33.699527, 73.073920),new LatLng(33.704257, 73.082993),new LatLng(33.707380, 73.088906),new LatLng(33.717143, 73.082961), new LatLng(33.718617, 73.084589), new LatLng(33.720660, 73.083891), new LatLng(33.727328, 73.073823), new LatLng(33.720397, 73.058392), new LatLng(33.733174, 73.087104)

                ));
        return allPoints;
    }

}

Class LocationDistances: binds location with distance.

public class LocationDistances {
    LatLng locs;
    double distances;
}

getOverAllRoute() method in MainActivity: which compares source Location with all the LatLngs returned from RouteData Class and Store them in a list srcLocDisList . All the trouble I am facing is in the Second loop which is calculating minimum distance and then returns the object associated with minimum distance but after one iteration the application crashes and while debugging it gives error "Source not found."?

public void getOverAllRoute(){
        //to get main route points to measure distance from
        RoutesData rD = new RoutesData();
        ArrayList<LatLng> mainRoutePoints = rD.allRoutesMainStops();
        //Toast.makeText(this,"Size: "+mainRoutePoints.size(), Toast.LENGTH_LONG).show();
        //to store location + distances from source

        ArrayList<LocationDistances> srcLocDisList = new ArrayList();
        //showMarkerslongLat();
        //create source Location
        Location srcLoc = new Location("");
        srcLoc.setLatitude(sll.latitude);
        srcLoc.setLongitude(sll.longitude);

        // to compare distances from source location
        Location mainPointsLoc = new Location("");
        for(int i =0;i<mainRoutePoints.size();i++){
            LocationDistances srcLocDis = new LocationDistances();
            mainPointsLoc.setLatitude(mainRoutePoints.get(i).latitude);
            mainPointsLoc.setLongitude(mainRoutePoints.get(i).longitude);
            //store distances and location in arraylist
            srcLocDis.locs = mainRoutePoints.get(i);
            srcLocDis.distances = srcLoc.distanceTo(mainPointsLoc);



            srcLocDisList.add(srcLocDis);
            Log.d("Location data: ",srcLocDis.locs.toString());
            Log.d("Saved Data: ",srcLocDisList.get(i).toString());

        }
        //Toast.makeText(this,"items1: "+srcLocDisList.size(), Toast.LENGTH_LONG).show();
        LocationDistances min=null;
        for(LocationDistances x:srcLocDisList){
            String srcLocDisLocFile = x.locs.latitude+" "+x.locs.longitude+" distances:"+x.distances;


            min=(min==null||x.distances<min.distances)?x:min;
            Log.d("LocDist:",Double.toString(x.distances));

        }
        LatLng srcStartMin=min.locs;
        Toast.makeText(this,srcStartMin.latitude+" "+srcStartMin.longitude+" Distance"+Double.toString(min.distances), Toast.LENGTH_LONG).show();

    }

Log file

Inside Second Loop:enter image description here

After the first Iterration: enter image description here

11
  • Can you please post your code for second loop and LogCat message Commented Apr 19, 2015 at 10:39
  • @dora I've editted the question. along log files Commented Apr 19, 2015 at 10:58
  • post the logcat please Commented Apr 19, 2015 at 11:00
  • @HeshanSandeepa log is at the end of the question Commented Apr 19, 2015 at 11:04
  • @dora the loop is supposed to initialize min with the minimum distance while debugging when it enters the loop the min is initilized Commented Apr 19, 2015 at 11:16

1 Answer 1

1

i think you should move the below line of code

LocationDistances srcLocDis = new LocationDistances();

into the for loop like below, otherwise the same object is being overwritten for the rest of the loop

for(int i =0;i<mainRoutePoints.size();i++){
        srcLocDis = new LocationDistances();
        mainPointsLoc.setLatitude(mainRoutePoints.get(i).latitude);
        mainPointsLoc.setLongitude(mainRoutePoints.get(i).longitude);
        //store distances and location in arraylist
        srcLocDis.locs = mainRoutePoints.get(i);
        srcLocDis.distances = srcLoc.distanceTo(mainPointsLoc);



        srcLocDisList.add(srcLocDis);
        Log.d("Location data: ",srcLocDis.locs.toString());
        Log.d("Saved Data: ",srcLocDisList.get(i).toString());

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.