Android Google Maps polyline not getting drawn without clearing ram

587 views Asked by At

I am developing a cab-like app using google maps.It has a pick up and drop location and clicking a button(GO) will draw the route between them. This is my activity

I have stored the last known location in shared preference.So,when the app is opened again the last known location is made the pick up location.

My problem is:

When i open the app again,the route is not getting drawn,but the route datum is downloaded from google. The LatLng datum is also not null.

But quite surprisingly,if I release my phone ram and open my app,ALAS IT WORKS!!!!

Things I have done so far:

1.I tried to garbage collect(in onDestroy();before drawing the route;after drawing the route, the first time).It didn't work.

2.I tried to clear the application cache but , all the application data including the last location that I have stored in the shared preference got deleted.The app starts from the first every time(i,e asking for runtime permission everytime;etc..)

3.I even tried deleting all data excluding my shared pref but found no success.

This is the code which gets called after downloading the route datum when the Go button is clicked.

  public void processFinish(List<List<HashMap<String, String>>> result) {
    Log.d("sri","processFinish in Map_Activity");


    Route_Extras.getting_json = false;

    ArrayList<LatLng> points = null;
    PolylineOptions polylineOptions = null;
    MarkerOptions markerOptions = new MarkerOptions();
    if(result != null) {

    if(result.size()<1){
        Toast.makeText(getBaseContext(), "No Points",Toast.LENGTH_SHORT).show();
        return;
    }

        //iterating routes
        for (int i = 0; i < result.size(); i++) {

            Log.d("sri", "Iterating routes " + distance);
            points = new ArrayList<LatLng>();
            polylineOptions = new PolylineOptions();

            //getting the individual route
            List<HashMap<String, String>> path = result.get(i);

            Log.d("sri", "Iterating every points in legs and steps " + distance);

            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);


                if (j == 0) {    // Get distance from the list
                    distance = (String) point.get("distance");
                    Log.d("sri", "Iterating leg " + j + "" + "distance is " + distance);
                    //  dist += Double.valueOf(distance);
                    continue;
                } 
                 else if (j == 1) { // Get duration from the list
                    duration = (String) point.get("duration");
                    Log.d("sri", "Iterating leg " + j + "" + "duration is " + duration);
                    //  dur += Double.valueOf(duration);
                    continue;
                }

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));

                LatLng position = new LatLng(lat, lng);

                points.add(position);

            }//path -->leg

            // Adding all the points in the route to LineOptions
            Log.d("sri", "Adding all the points in the route to LineOptions");
            polylineOptions.addAll(points);
            polylineOptions.width(6);
            polylineOptions.color(Color.BLUE);

        }//route

        Log.d("sri", "Drawing polyline in the Google Map ");


        // Drawing polyline in the Google Map for the i-th route
        if(polylineOptions!=null){        
         pline = mGoogleMap.addPolyline(polylineOptions);
            if(pline != null){
                Log.d("sri", "if(pline!=null)");
            }

            Log.d("sri", "if(polylineOptions!=null)");
        }

           else{
            Log.d("sri", "if(polylineOptions == null)");
        }
    }
}//process

I am facing this issue since last 10 days and googled it atleast 100 times and found no solution and so posted it in SOO.

1

There are 1 answers

2
Sukumar Nagarajan On

Try this:

PolylineOptions rectLine = new PolylineOptions().width(15).color(ContextCompat.getColor(this,R.color.map_route));
    for (int i = 0; i < directionPoints.size(); i++) {
        rectLine.add(directionPoints.get(i));
    }
    if (newPolyline != null) {
        newPolyline.remove();
    }
    newPolyline = map.addPolyline(rectLine);