title: Android google map single polyline or multiple
body: <p>I have a best practices question on how to manage adding a polyline path to a GoogleMap in android, that grows as the user moves.</p>
  
  <p><strong>TLDR version</strong>: Is it better to keep replacing the polyline with a new one that is all the previous points plus the new one, or to keep adding a ton of 2 point polylines?</p>
  
  <p>In previous iterations, I've created a new 2 point polyline for every new location, where the start of it is at the end of the previous line, and the end of it at the current location.</p>
  
  <p>I came to the realization that I could be creating MANY resources for the map to have to keep track of instead of just one. Because of this, I've looked into how to make a single line and just add points to it (that doesn't seem to work), and there isn't any documentation that I can find that says how I might achieve this.</p>
  
  <p>I have currently landed on this solution:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>private lateinit var polyLine: Polyline
  fun updateLocation(location: Location?) {
      location?.let {
          val latLng = LatLng(location!!.latitude, location!!.longitude)
          if (firstLoc) {
              polyLine = googleMap.addPolyline(PolylineOptions().add(latLng))
              googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16F))
              firstLoc = false;
          } else {
              googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16F))
              val newLine = googleMap.addPolyline(PolylineOptions().addAll(polyLine.points).add(latLng).width(10f))
              polyLine.remove()
              polyLine = newLine
          }
      }
  }
  </code></pre>
  
  <p>Which allows the map to only have to deal with one line at a time. My main question is whether or not this is worse, due to having to recreate all the points on the new line every time?
  Running in my emulator, and my actual device, it seems fine. There's no flickering (which makes sense, it's adding the new line before removing the old one), and there doesn't appear to be any lag.</p>
  
  <p>My current plan is to continue how it is, because it's simple, and appears to work. But I'd want to make sure I'm not causing more work than I need. It seems like there's a bit of work that needs to happen in either case, but this way seemed to me to be the better route.</p>