Search code examples
androidgoogle-mapspolyline

Android Adding a polyline to map error


My application retrieve a JSON from URL and parse the directions from the user's location to the selected marker's location. The debug shows me that the destination and the source are correct, by the way the app gets stuck when I add the polyline in the map. Here is the code:

for(int z = 0; z<list.size()-1;z++){
    LatLng src= list.get(z);
    LatLng dest= list.get(z+1);

    PolylineOptions line=  new PolylineOptions()
        .add(src, dest)
        .width(2)
        .color(Color.BLUE).geodesic(true);
        mMap.addPolyline(line);
}

And here is the edited logcat:

05-20 11:44:04.698 26190-27044/com.example.marco.progettoquinta E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
  Process: com.example.marco.progettoquinta, PID: 26190
  java.lang.RuntimeException: An error occurred while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:309)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
      at java.lang.Thread.run(Thread.java:818)
   Caused by: java.lang.IllegalStateException: Not on the main thread
      at maps.f.g.b(Unknown Source)
      at maps.z.D.a(Unknown Source)
      at maps.ag.t.a(Unknown Source)
      at uz.onTransact(:com.google.android.gms.DynamiteModulesB:137)
      at android.os.Binder.transact(Binder.java:387)
      at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addPolyline(Unknown Source)
      at com.google.android.gms.maps.GoogleMap.addPolyline(Unknown Source)
      at com.example.marco.progettoquinta.MapsActivity.drawPath(MapsActivity.java:210)
      at com.example.marco.progettoquinta.JSONParser.getJSONFromUrl(JSONParser.java:80)
      at com.example.marco.progettoquinta.JSONParser.doInBackground(JSONParser.java:35)
      at com.example.marco.progettoquinta.JSONParser.doInBackground(JSONParser.java:18)
      at android.os.AsyncTask$2.call(AsyncTask.java:295)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
      at java.lang.Thread.run(Thread.java:818) 

It says that the source is unknown but it's actually correct. Of course this method is implemented in a AsyncTask. Where is the error?


Solution

  • Your exception occurs because you're trying to modify the UI not from the main thread but from the doInBackground of your asynctask (which is required for your request). Try encapsulating your code into something like this

    runOnUiThread(new Runnable() {
    
       @Override
       public void run()
       {
          //do your loop adding polyline
       }
    });
    

    or get your data returned from your doInBackground and process it to add the polylines in the OnPostExecute.