title: How to Draw Many Polylines in Google Map Efficiently?
body: <p>I am currently learning to make an android app that uses Google Map to display data to the users. Currently I am using Polyline to draw out all the "data/lines" that I want to show on the map, but I draw it straight on the Google Map, like mMap.addPolyline(...). (I wish I can post a screenshot but I don't have at least 10 reputation :-( ).</p>
  
  <p>But I am having some performance issues over drawing all these lines on the map, because they consists of like 500 ~ 900 lines in a small area there (by zooming in). The issue is that when the app is drawing all these data, the app hang/stop for a second, like froze, then once it is finished drawing, the app continue to work.</p>
  
  <p>So my question is, how can I draw that many Polylines smoothly without having the app to hang/stop for a second when it is drawing on the map? Such as the user can move around the map while the Polylines are being drawn on the map? </p>
  
  <p>I had tried on drawing Polylines on the map in a different thread, but I got an error something like must draw on the main thread (can't remember the exact Exception error). Other than that, I also searched over the Internet for example like this, but I only found the example with Overlay or ItemizedOverlay things, but those are deprecated as far as I know. Is there any other options for doing this? Can you show me some examples or guide me to the right resources? Showing me some code would be very helpful.</p>
  
  <p>Thank you</p>
  
  <p><strong>Update:</strong>
  Forgot to include my code here, and here is the code that I draw the Polylines:</p>
  
  <pre><code>private class LoadDataTask extends AsyncTask&lt;Void, Void, Void&gt; {
  
      @Override
      protected void onPreExecute() {
          // Show a message saying this app is fetching data
          Toast.makeText(mMainActivity,
                  "Fetching data ...",
                  Toast.LENGTH_SHORT)
                  .show();
      }
  
      @Override
      protected Void doInBackground(String... urls) {
          // Load then read the data from a url in the background
          loadData();
  
          readData();
  
          return null;
      }
  
      @Override
      protected void onPostExecute(Void unused) {
  
          // draw lines here
          PolylineOptions lineOptions = new PolylineOptions()
                  .add(new LatLng(locations[0].getLatitude(), locations[0].getLongitude()))
                  .add(new LatLng(locations[1].getLatitude(), locations[1].getLongitude()))
                  .color(Color.GREEN)
                  .width(5f);
  
          Polyline polyline = mMap.addPolyline(lineOptions);
          lineList.add(polyline);
  
      }
  }
  </code></pre>