在 Swift 中渲染 MKPolyline

Rendering MKPolyline in Swift

我想在 MapKit 上简单地创建一条直线。我(看似)成功创建了折线,因为构建成功且没有错误。但是,该线仍未呈现。

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()
  }
}

您可能需要 viewDidLoad

self.mapView.delegate = self