Search code examples
iosswiftmkmapviewmkoverlaycoordinate

How to add overlay path in MKMapView swift


I want to add a overlay path among multiple coordinates in mapview. I tried like below code, but it shows a error of "cannot invoke 'map' with an argument list of type ((CLLocation) -> CLLocationCoordinate2D)". Please let me know how can i fix this ?

My ViewController.swift file

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate{
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //For Location 1
        let location1 = CLLocationCoordinate2D(
            latitude: 51.481188400000010000,
            longitude: -0.190209099999947280
        )

        let annotation1 = MKPointAnnotation()
        annotation1.coordinate = location1;
        annotation1.title = "Chelsea"
        annotation1.subtitle = "Chelsea"

        let span = MKCoordinateSpanMake(0.15, 0.15)

        let region1 = MKCoordinateRegion(center: location1, span: span)
        mapView.setRegion(region1, animated: true)
        mapView.addAnnotation(annotation1)

        //For Location 2
        let location2 = CLLocationCoordinate2D(
            latitude: 51.554947700000010000,
            longitude: -0.108558899999934510
        )

        let annotation2 = MKPointAnnotation()
        annotation2.coordinate = location2;
        annotation2.title = "Arsenal"
        annotation2.subtitle = "Arsenal"

        let region2 = MKCoordinateRegion(center: location1, span: span)
        mapView.setRegion(region2, animated: true)
        mapView.addAnnotation(annotation2)

        var locations = [CLLocation(latitude: 51.481188400000010000, longitude: -0.190209099999947280), CLLocation(latitude: 51.554947700000010000,longitude:  -0.108558899999934510)]

        //This line shows error
        var coordinates = locations.map({(location: CLLocation) -> CLLocationCoordinate2D in return location.coordinate})

        var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

        mapView.addOverlay(polyline)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolyline {
            var polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.strokeColor = UIColor.blueColor()
            polylineRenderer.lineWidth = 5
            return polylineRenderer
        }

        return nil
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Solution

  • This should work:

    var coordinates = locations.map {
        location in
        return location.coordinate
    }
    

    One-liner:

    var coordinates = locations.map { $0.coordinate }
    

    The problem with your code was that locations is a variable of type [CLLocation!] (note the exclamation mark here), but you are declaring its elements as CLLocation (without the !) in the closure:

    (location: CLLocation) -> CLLocationCoordinate2D