DSmith
DSmith

Reputation: 159

Rendering MKPolyline in Swift

I am trying to simply create a straight line on MapKit. I (seemingly) successfully created the polyline, as the build succeeds without errors. However, the line is still not rendered.

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate{

  @IBOutlet weak var mapView: MKMapView!
  override func viewDidLoad() {
      super.viewDidLoad()

      let point1 = CLLocationCoordinate2DMake(37.558359, -77.483795);
      let point2 = CLLocationCoordinate2DMake(37.558062, -77.482958);

      let points: [CLLocationCoordinate2D]
      points = [point1, point2]

      let polyline = MKPolyline(coordinates: &points, count: points.count)
      self.mapView.addOverlay(polyline)
 }
}
extension ViewController {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> 
  MKOverlayRenderer {
    if overlay is MKPolyline {
        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = UIColor.orange
        renderer.lineWidth = 3
        return renderer
    }

    return MKOverlayRenderer()
  }
}

Upvotes: 1

Views: 318

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You may need in viewDidLoad

self.mapView.delegate = self

Upvotes: 2

Related Questions