1.
MKPolyLine
is a class which holds multiple map-coordinates to define the shape of a polyline, very near to just an array of coordinates.
MKPolyLineView
is a view class which manages the visual representation of the MKPolyLine
. As mentioned in the Kane Cheshire's answer, it's outdated and you should use MKPolyLineRenderer
.
Which one should be used when?
You need to use both MKPolyLine
and MKPolyLineRenderer
as in the code below.
2.
MKPolyLine
has two initializers:
When you want to pass [CLLocationCoordinate2D]
to MKPolyLine.init
, you need to use init(coordinates:count:)
.
(Or you can create an Array of MKMapPoint
and pass it to init(points:count:)
.)
And when you want to pass an immutable Array (declared with let
) to UnsafePointer
(non-Mutable), you have no need to prefix &
.
The code below is actually built and tested with Xcode 9 beta 3:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let coords1 = CLLocationCoordinate2D(latitude: 52.167894, longitude: 17.077399)
let coords2 = CLLocationCoordinate2D(latitude: 52.168776, longitude: 17.081326)
let coords3 = CLLocationCoordinate2D(latitude: 52.167921, longitude: 17.083730)
let testcoords:[CLLocationCoordinate2D] = [coords1,coords2,coords3]
let testline = MKPolyline(coordinates: testcoords, count: testcoords.count)
//Add `MKPolyLine` as an overlay.
mapView.add(testline)
mapView.delegate = self
mapView.centerCoordinate = coords2
mapView.region = MKCoordinateRegion(center: coords2, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02))
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
//Return an `MKPolylineRenderer` for the `MKPolyline` in the `MKMapViewDelegate`s method
if let polyline = overlay as? MKPolyline {
let testlineRenderer = MKPolylineRenderer(polyline: polyline)
testlineRenderer.strokeColor = .blue
testlineRenderer.lineWidth = 2.0
return testlineRenderer
}
fatalError("Something wrong...")
//return MKOverlayRenderer()
}
}