title: How do I create a line (polyline) between location points in Swift?
body: <p>I am trying to make an app that tracks a user's location and creates a line following where the user went.  I have created a bunch of annotations following where the user went, but can't figure out how to create a line between them.  I am new to swift and Xcode and I have looked all over the internet but can't find anything that works.  <a href="https://i.stack.imgur.com/mfJcd.png" rel="nofollow noreferrer">Picture of app with annotations following user location</a></p>
  
  <p>Here is my code: </p>
  
  <pre><code>import UIKit
  import MapKit
  import CoreLocation
  
  class ViewController: UIViewController, CLLocationManagerDelegate {
      @IBOutlet var mapView: MKMapView?
  
      //Map
      @IBOutlet weak var map: MKMapView!
      @IBOutlet weak var lblSpeed: UILabel!
  
      let manager = CLLocationManager()
  
      @IBOutlet weak var lblAltitude: UILabel!
  
      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          let location = locations[0]
  
          let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
  
          let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
  
          let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
  
          lblAltitude.text = String(format:"%.2f", location.altitude)
          lblSpeed.text = String(format:"%.2f", location.speed)
  
          self.map.showsUserLocation = true
  
          var testLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
  
          var annotation = MKPointAnnotation()
          annotation.coordinate = testLocation
          map.addAnnotation(annotation)
  
          var locationTest = [CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)]
          var coordinates = locations.map({(location: CLLocation!) -&gt; CLLocationCoordinate2D in return location.coordinate})
          var polyline = MKPolyline(coordinates: &amp;testLocation, count: locations.count)
  
          func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -&gt; MKOverlayRenderer! {
              if overlay is MKPolyline {
                  let polylineRenderer = MKPolylineRenderer(overlay: overlay)
                  polylineRenderer.strokeColor = UIColor.blue
                  polylineRenderer.lineWidth = 5
                  return polylineRenderer
              }
  
              return nil
          }
      }
  </code></pre>