在Swift中渲染MKPolyline

时间:2019-01-17 19:26:00

标签: ios swift mkpolyline

我试图在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()
  }
}

1 个答案:

答案 0 :(得分:2)

您可能需要viewDidLoad

self.mapView.delegate = self