ios - How to add overlay path in MKMapView swift -
i want add overlay path among multiple coordinates in mapview. tried below code, shows error of "cannot invoke 'map' argument list of type ((cllocation) -> cllocationcoordinate2d)". please let me know how can fix ?
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 mkpolyline { var polylinerenderer = mkpolylinerenderer(overlay: overlay) polylinerenderer.strokecolor = uicolor.bluecolor() polylinerenderer.linewidth = 5 return polylinerenderer } return nil } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } }
this should work:
var coordinates = locations.map { location in return location.coordinate }
one-liner:
var coordinates = locations.map { $0.coordinate }
the problem code locations
variable of type [cllocation!]
(note exclamation mark here), declaring elements cllocation
(without !) in closure:
(location: cllocation) -> cllocationcoordinate2d
Comments
Post a Comment