使用Swift绘制折线

我想了解如何使用Swift绘制多段线。 我查看了文档,引用了一些教程,并检查了一些其他的SOpost,但我仍然无法在我的地图上画线。 这是我的代码。 有人告诉我我在这里做错了什么?

import UIKit import MapKit class FirstViewController: UIViewController { @IBOutlet weak var map: MKMapView! override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let location = CLLocationCoordinate2D( latitude: -73.761105, longitude: 41.017791 ) let span = MKCoordinateSpanMake(0.07, 0.07) let region = MKCoordinateRegion(center: location, span: span) map.setRegion(region, animated: true) let annotation = MKPointAnnotation() annotation.setCoordinate(location) annotation.title = "White Plains" annotation.subtitle = "Westchester County" map.addAnnotation(annotation) var locations = [CLLocation(latitude: -73.761105, longitude: 41.017791), CLLocation(latitude: -73.760701,longitude: 41.019348), CLLocation(latitude: -73.757201, longitude: 41.019267), CLLocation(latitude: -73.757482, longitude: 41.016375), CLLocation(latitude: -73.761105, longitude: 41.017791)] var coordinates = locations.map({(location: CLLocation!) -> CLLocationCoordinate2D in return location.coordinate}) var polyline = MKPolyline(coordinates: &coordinates, count: locations.count) self.map.addOverlay(polyline) } func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! { if overlay is MKPolyline { var polylineRenderer = MKPolylineRenderer(overlay: overlay) polylineRenderer.strokeColor = UIColor.blueColor() polylineRenderer.lineWidth = 5 return polylineRenderer } return nil } } 

谢谢!

这里MKGeodesicPolyline会解决你的问题。 添加MKGeodesicPolyline而不是MKPolyline对象。

在你的代码中删除下面两行:

  let polyline = MKPolyline(coordinates: &coordinates, count: locations.count) map.add(polyline) 

并添加这些行:

  let geodesic = MKGeodesicPolyline(coordinates: coordinates, count: 2) map.add(geodesic) 

SWIFT代码:

 func createPolyline(mapView: MKMapView) { let point1 = CLLocationCoordinate2DMake(-73.761105, 41.017791); let point2 = CLLocationCoordinate2DMake(-73.760701, 41.019348); let point3 = CLLocationCoordinate2DMake(-73.757201, 41.019267); let point4 = CLLocationCoordinate2DMake(-73.757482, 41.016375); let point5 = CLLocationCoordinate2DMake(-73.761105, 41.017791); let points: [CLLocationCoordinate2D] points = [point1, point2, point3, point4, point5] let geodesic = MKGeodesicPolyline(coordinates: points, count: 5) map.add(geodesic) UIView.animate(withDuration: 1.5, animations: { () -> Void in let span = MKCoordinateSpanMake(0.01, 0.01) let region1 = MKCoordinateRegion(center: point1, span: span) self.map.setRegion(region1, animated: true) }) } 

目标C代码:

 - (void) createGeoPolyline { CLLocationCoordinate2D point1 = { -73.761105, 41.017791 }; CLLocationCoordinate2D point2 = { -73.760701, 41.019348 }; CLLocationCoordinate2D point3 = { -73.757201, 41.019267 }; CLLocationCoordinate2D point4 = { -73.757482, 41.016375 }; CLLocationCoordinate2D point5 = { -73.761105, 41.017791 }; CLLocationCoordinate2D points[] = {point1, point2, point3, point4, point5}; MKGeodesicPolyline *geodesic = [MKGeodesicPolyline polylineWithCoordinates:&points[0] count:5]; [self.mapView addOverlay:geodesic]; [UIView animateWithDuration:1.5 animations:^{ MKCoordinateRegion region; region.center = point1; MKCoordinateSpan span; span.latitudeDelta = 0.01; span.longitudeDelta = 0.01; region.span = span; [self.mapView setRegion:region animated:YES]; }]; } 

以上目标C代码工作完美,它会显示下面的重叠:

在这里输入图像说明

但是,如果你尝试Swift代码,它不会。 我尽我所能地解决它,但它不会改变。 可能是来自MapKit框架的bug。

更新:这似乎是固定的Swift 3+。 看到接受的答案。

在这一行上:

 var polyline = MKPolyline(coordinates: &coordinates, count: locations.count) 

您正在将一个Swift数组引用作为UnsafePointer<CLLocationCoordinate2D>进行UnsafePointer<CLLocationCoordinate2D>

这很危险,我不确定为什么Swift允许它编译。 最好的情况下,你得到的线,更糟糕的情况下(这似乎是你的情况),你什么也得不到。

MKPolyline构造函数需要一个UsafePointer<CLLocationCoordinate2D> ,这就是你应该通过的。

我通常添加一个私人类别MKPolyline创build一个方便的init方法,接受一个普通的Swift数组:

 private extension MKPolyline { convenience init(coordinates coords: Array<CLLocationCoordinate2D>) { let unsafeCoordinates = UnsafeMutablePointer<CLLocationCoordinate2D>.alloc(coords.count) unsafeCoordinates.initializeFrom(coords) self.init(coordinates: unsafeCoordinates, count: coords.count) unsafeCoordinates.dealloc(coords.count) } }