1

I wrote this method to determine a distance to a destination along a path and also to determine a distance to the next waypoint along that path. The problem is, sometimes I don't go within 10 meters of the waypoint so it's not getting caught as having been reached. Sure, I could increase the threshold of distance that would make the waypoint met, but I'd rather come up with another solution to determine if I've passed that waypoint and just skip it (by adding it to the waypointsReached list) to move on to the next one. Checking to see if the distance from my location to the waypoint is increasing instead of decreasing won't work because I may stray off the path before I even get to the waypoint.

Can anyone help me wrap my head around an idea of a way I can determine if I've just passed the waypoint and move on to the next one?

Here's the method I've written to get my distance to the final destination and to the next waypoint.

    // method to calculate distance to destinatoin from a loaded route
private void CalculateRouteToDestination(Location location) {

    new AsyncTask<Location, Void, ArrayList<Double>>(){
        @Override
        protected ArrayList<Double> doInBackground(Location... params) {
            ArrayList<Double> distances = new ArrayList<Double>();
            Location location = params[0];
            //predefinedRoutePoints
            int closestLocationIndex = 0;
            double smallestDistance = -1;
            int cnt = 0;
            for(LatLng ltlg : predefinedRoutePoints) {
                // create  new location object for the latlng coords
                Location cLocation = new Location("");
                cLocation.setLatitude(ltlg.latitude);
                cLocation.setLongitude(ltlg.longitude);
                // get the distance from current location to all of the points in the array and set the index from the array where it is
                double cDistance = location.distanceTo(cLocation);
                if(smallestDistance == -1 || cDistance < smallestDistance) {
                    closestLocationIndex = cnt;
                    smallestDistance = cDistance;
                }
                cnt++;
            }
            // now with the index from the array of the cloest point, calculate the distance to the last point in the array from that index (distance to destination)
            double distanceToDestination = 0;
            for(int i = closestLocationIndex; i < predefinedRoutePoints.size(); i++) {
                //Log.d("home","iteration:"+i);
                if(i != (predefinedRoutePoints.size() -1)) {
                    Location l1 = new Location("");
                    l1.setLatitude(predefinedRoutePoints.get(i).latitude);
                    l1.setLongitude(predefinedRoutePoints.get(i).longitude);

                    Location l2 = new Location("");
                    l2.setLatitude(predefinedRoutePoints.get(i+1).latitude);
                    l2.setLongitude(predefinedRoutePoints.get(i+1).longitude);
                    distanceToDestination += l1.distanceTo(l2);

                }
            }
            // now add the distance the user is to the cloest point to get the final output of distance to destinatoin
            distanceToDestination += smallestDistance;
            distances.add(distanceToDestination);

            // check if need to measure distance to next waypoint
            double distanceToNextWaypoint = 0;
            if(predefinedWaypoints.size() > 0){
                if(predefinedWaypoints.size() != waypointsReached.size()) {
                    for (int i = closestLocationIndex; i < predefinedWaypoints.get(waypointsReached.size()).WaypointIndex; i++) {
                        Location l1 = new Location("");
                        l1.setLatitude(predefinedRoutePoints.get(i).latitude);
                        l1.setLongitude(predefinedRoutePoints.get(i).longitude);

                        Location l2 = new Location("");
                        l2.setLatitude(predefinedRoutePoints.get(i + 1).latitude);
                        l2.setLongitude(predefinedRoutePoints.get(i + 1).longitude);
                        distanceToNextWaypoint += l1.distanceTo(l2);
                    }

                    distanceToNextWaypoint += smallestDistance;
                    if(distanceToNextWaypoint < 10) {
                        waypointsReached.add(predefinedWaypoints.get(waypointsReached.size()));

                    }
                    distances.add(distanceToNextWaypoint);
                }
            }
            return distances;
        }
        @Override
        protected void onPostExecute(ArrayList<Double> v) {
            distanceToDestinationText.setText("Destination To Destination: "+Utilities.GetMiles(v.get(0))+"mi");
            if(v.size() > 1) {
                distanceToNextWaypoint.setText("Next Waypoint: "+Utilities.GetMiles(v.get(1))+"mi");
            } else {
                distanceToNextWaypoint.setVisibility(View.GONE);
                distanceToNextWaypoint.setText("Next Waypoint: 0.0mi");
            }
        }
    }.execute(location);
}
Blair Holmes
  • 1,521
  • 2
  • 22
  • 35

0 Answers0