MKPolyLine没有出现在Swift的MapView上

前端之家收集整理的这篇文章主要介绍了MKPolyLine没有出现在Swift的MapView上前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Swift中有以下代码将MKPolyline添加到MapView. XCode没有告诉我有一个问题,据我所知,这应该是有效的.

MapView的插座:

@IBOutlet weak var mapView: MKMapView!

用于保持坐标的变量:

var coordinates: [CLLocationCoordinate2D] = []

从Core Data获取我保存的坐标:

var contextMap = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!
var requestMap = NSFetchRequest(entityName: "Locations")
let predMap = NSPredicate(format: "game = %d",passedGameNumber)
requestMap.predicate = predMap
requestMap.sortDescriptors = [NSSortDescriptor(key:"time",ascending: false)]
    self.locationsList = contextMap.executeFetchRequest(requestMap,error: nil)! as [Locations]

将Core Data中的坐标添加到我的新数组中:

for index in 1..<self.locationsList.count{
    var lat = Double(self.locationsList[index].latitude)
    var long = Double(self.locationsList[index].longitude)
    var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat,longitude: long)
    coordinates.append(coordinatesToAppend)
}

创建折线:

var polyLine = MKPolyline(coordinates: &coordinates,count: coordinates.count)

添加叠加层:

self.mapView.addOverlay(polyLine,level: MKOverlayLevel.AboveRoads)

将其添加到MapView:

func mapView(mapView: MKMapView!,rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if overlay.isKindOfClass(MKPolyline) {
        // draw the track
        let polyLine = overlay
        let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
        polyLineRenderer.strokeColor = UIColor.blueColor()
        polyLineRenderer.lineWidth = 2.0

        return polyLineRenderer
    }

    return nil
}

我只是得到一个空白的MapView.我可以将坐标数组打印到控制台,所以我知道数据已被添加.有任何想法吗?

正如评论中所述,问题中的代码很好.我只是没有设置代表.
mapView.delegate = self

猜你在找的Swift相关文章