androidcoordinatesgoogle-maps-android-api-2directionsmap-directions

Google Maps Directions API showing wrong directions


I am using the Google Maps API and the Google Maps Directions API for tracking the journey of a pilgrimage. My travel mode is walking and I have 8 waypoints placed along the way (Google doesn't allow you to add more for free).

The problem is that Google Maps is taking an odd unsought detour from the original path. I set up LatLng coordinates as waypoints accordingly to fix the problem, but the API remains stubborn on its resolve to take me 200 km away from the path and back again...

I have attached a screenshot of the app with this question. As you can see, in the bottom right corner, Google Maps takes a detour. How do I fix the problem? Thank You.enter image description here

Here's some code:

String url = "https://maps.googleapis.com/maps/api/directions/json?origin=18.6769503," +
            "73.8945701&destination=17.6774444,75.3329239&mode=walking&waypoints=" +
            "via:Saswad,MH|via:Jejuri,MH|via:Natepute,MH|" +
            "via:Velapur,MH|via:Bhandishegaon,MH|via:Wakhari,MH|" +
            "via:17.685236,75.287492|via:17.682496,75.304931&" +
            "key=MY_API_KEY";

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    StringRequest jsonObjectRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.i("Json Data", "Successfully Retrieved - Starting Parse Process");
                    mapJsonData = response;
                    try {
                        parseJsonData(mapJsonData);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    PolylineOptions polyOptions = new PolylineOptions().geodesic(true).
                            color(Color.RED).
                            width(10);
                    polyOptions.add(dnyaneshwarOrigin);
                    for (int i = 0; i < polyPoints.size(); i++) {
                        polyOptions.add(polyPoints.get(i));
                    }
                    polyOptions.add(vitthalMandir);
                    gMap.addPolyline(polyOptions);

                    final LatLngBounds.Builder builder = new LatLngBounds.Builder();
                    builder.include(dnyaneshwarOrigin);
                    builder.include(vitthalMandir);

                    mapLayout.getViewTreeObserver().addOnGlobalLayoutListener
                            (new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            LatLngBounds bounds = builder.build();
                            gMap.animateCamera(CameraUpdateFactory.newLatLngBounds
                                    (bounds, 20));
                        }
                    });
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("Response Error", "Could Not Retrieve Json Data");
            Toast.makeText(MapActivity.this, "Error Retrieving Json Data",
                    Toast.LENGTH_SHORT).show();
        }
    });
    Log.i("Request Queue", "Sending Json Request");
    requestQueue.add(jsonObjectRequest);

Solution

  • Fixed the Problem. There was an alternative place with the same name as one of my way-points. I changed the way-point in my URL from a name to specific Lat-Long Coordinates and that solved the problem for me.