2
votes

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.
    }
}
1
The answer is useful but note that you don't even need to first create an array of CLLocations and then map them to an array of CLLocationCoordinate2Ds. You can create an array of CLLocationCoordinate2Ds directly: var coordinates = [location1, location2]. Also don't need to repeat coordinate values -- use the variables that already have the values (location1, location2).user467105
Minor side detail: region2 center is using location1 instead of location2. However, you don't need to do setRegion to add an annotation at a location. Here, only the second setRegion call has an effect on the display.user467105

1 Answers

2
votes

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