I have an array of CLLocationCoordinate2D, and need to draw a line connecting from the first index to the last index in the array.
I have seen the answers here and elsewhere, but they are mostly for older versions of swift, or for an array of strings that you first convert to doubles or whatever or for only a single location.
I have tried creating an MKPolyline, viewForOverlay, etc, but I am unable to get the line to show up. What am I missing?
let locationManager = CLLocationManager()
let regionRadius: CLLocationDistance = 3000
var locations: [CLLocationCoordinate2D] = []
var latitude: [CLLocationDegrees] = []
var longitude: [CLLocationDegrees] = []
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.register(MKMarkerAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
createAnnotations()
centerMapOnLocation(location: initialLocation)
var polyline = MKPolyline(coordinates: &locations, count: locations.count)
mapView.addOverlay(polyline)
}
// MARK: - Create Annotaions
func createAnnotations() {
locations = zip(latitude, longitude).map(CLLocationCoordinate2D.init)
AppLogger.logInfo("\(locations)")
for location in locations {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
mapView.addAnnotation(annotation)
}
}
// MARK: - Region Zoom
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegion(center: location.coordinate,
latitudinalMeters: regionRadius,
longitudinalMeters: regionRadius)
mapView.setRegion(coordinateRegion, animated: true)
}
// MARK: - Draw Polylines
func mapView(mapView: MKMapView!, viewForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
if (overlay is MKPolyline) {
let pr = MKPolylineRenderer(overlay: overlay)
pr.strokeColor = UIColor.blue.withAlphaComponent(0.5)
pr.lineWidth = 5
return pr
}
return nil
}
Expected output: From the array of locations we essentially draw the route driven. It's not a get directions method as the drive was already taken.