0

I am trying to add a Check in my app to follow predefined route map.I am using isLocationOnPath method but its showing me Toast of "Please follow route" even when I am following the route.

Below I have my code:

    private void StartCheckingForQuestionInRadius() {

        if (visitedPoints == null) {
            visitedPoints = new ArrayList<LatLng>();
        }


        try {
            final Runnable r = new Runnable() {
                public void run() {
                    if (handelrCheck) {
                        if (visitedPoints.size() != allQuestions.size() && QuestionOnTheRoute() < mCircle.getRadius() && !isDone() && Integer.parseInt(allQuestions.get(questionNumber).getSequence())==sequenceFlag) {
                            visitedPoints.add(new LatLng(Double.parseDouble(allQuestions.get(questionNumber).getLat()), Double.parseDouble(allQuestions.get(questionNumber).getLng())));
                            showPopup(); // show question pop up when it is inside user radius
                            //add viisited check point to menu
                            VisitedcheckPoint++;
                            sequenceFlag++;
                            heading5.add("" + VisitedcheckPoint);
                            for (int i = 0; i < heading5.size(); i++) {
                                if (heading5.size() > 1) {
                                    heading5.remove(0);
                                }
                            }
                        }

                        else if (checkCompleteSurvey()<mCircle.getRadius()) { // when user complete survey
                            handelrCheck = false;
                            handlerForQuestions = null;
                            showAllAnswers(); // show all ansers to user when user complete survey

                        }
                        if (!PolyUtil.isLocationOnPath(new LatLng(AppPreferences.getLatitude(mCurrentActivity),AppPreferences.getLongitude(mCurrentActivity)),routePoints,true,50)){
                            Toast.makeText(mCurrentActivity, "Please follow the route", Toast.LENGTH_SHORT).show();
                        }

//                        else if (visitedPoints.size() == databaseHandler.getQuestionOnRoute().size()) { // when user complete survey
//                            handelrCheck = false;
//                            handlerForQuestions = null;
//                            showAllAnswers(); // show all ansers to user when user complete survey
//
//                        }
                    }

                    if (handlerForQuestions != null)
                        handlerForQuestions.postDelayed(this, pooltime);
                }

            };

            handlerForQuestions.postDelayed(r, pooltime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I am calling this method in the Event as below:

 @Subscribe
    public void onEvent(String action) {
        if (action.equalsIgnoreCase(PreferencesKeys.LOCATION_UPDATED)) {
            firstTime = true;
            firstTime = true;
            myLocation = new LatLng(AppPreferences.getLatitude(mCurrentActivity), AppPreferences.getLongitude(mCurrentActivity));
            if (mCircle != null) {
                mCircle.remove();
            }
            drawMarkerWithCircle(myLocation);
            if (firstTime) {
                //       animateCameraTo(AppPreferences.getLatitude(mCurrentActivity), AppPreferences.getLongitude(mCurrentActivity));
            }
            if (handelrCheck && handlerForQuestions == null) {
                handlerForQuestions = new Handler();
                StartCheckingForQuestionInRadius();
            }
        }
    }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Usman
  • 11
  • 4
  • Could you show an example of the values of `new LatLng(AppPreferences.getLatitude(mCurrentActivity),AppPreferences.getLongitude(mCurrentActivity))` and `routePoints`? – antonio Mar 20 '17 at 12:44
  • routePoints = [lat/lng: (32.51688,74.50946), lat/lng: (32.51723,74.50999), lat/lng: (32.51892,74.50786), lat/lng: (32.51938,74.50729), lat/lng: (32.51944,74.50718), lat/lng: (32.51963,74.50682), lat/lng: (32.51986,74.50596)] – Usman Mar 20 '17 at 12:51
  • new Latlong(lat/lng: (32.51674666666667,74.5096)) – Usman Mar 20 '17 at 12:53
  • `PolyUtil.isLocationOnPath(new LatLng(32.51674666666667,74.5096), routePoints, true, 50)` returns `true` – antonio Mar 20 '17 at 12:57
  • i did't get your point – Usman Mar 20 '17 at 13:01
  • Using the parameters you give, `isLocationOnPath` returns `true` as expected, so your problem is somewhere else. You should try to isolate your problem to solve it – antonio Mar 20 '17 at 13:03
  • After changing travel mode to Cycle it show me toast for a while and then disappear same thing happens continuously – Usman Mar 20 '17 at 13:08
  • What i think..Its checking only for first point of RoutePoints list – Usman Mar 20 '17 at 13:15
  • When i get 50 Meters away from my start point its shows me toast of follow location. – Usman Mar 20 '17 at 13:17
  • And what's the value of `new LatLng(AppPreferences.getLatitude(mCurrentActivity),AppPreferences.getLongitude(mCurrentActivity))` when you are 50 Meters away from your start point? – antonio Mar 20 '17 at 13:18
  • These Values are updating to current location – Usman Mar 20 '17 at 13:28
  • Maybe the problem is in the value of your "current location" then, as PolyUtil.isLocationOnPath is a well tested function. You may want to take a look at http://stackoverflow.com/questions/37136981/draw-path-on-google-map-based-on-lat-lang/37682382#37682382 – antonio Mar 20 '17 at 13:45
  • Could you replace the PolyUtil check with this and tell us what's in the Toast Message ? if (!PolyUtil.isLocationOnPath(new LatLng(AppPreferences.getLatitude(mCurrentActivity),AppPreferences.getLongitude(mCurrentActivity)),routePoints,true,50)){ Toast.makeText(mCurrentActivity, "" +AppPreferences.getLatitude(mCurrentActivity)+AppPreferences.getLongitude(mCurrentActivity), Toast.LENGTH_SHORT).show(); } – throws_exceptions_at_you Mar 22 '17 at 16:03
  • Now the problem is that when i choose straight path then it works fine and when i don't have straight path then it gives me message of follow path – Usman Mar 24 '17 at 06:17

0 Answers0