用MKMapView绘制路径问题

时间:2016-05-17 12:02:08

标签: swift nsmutableset

我正在尝试绘制一条路径(MKMapView)。但我有:

  

NSInvalidArgumentException',原因:'*** - [NSMutableOrderedSet addObject:]:object不能为nil'。

我有一个NSTimer按钮操作,调用函数stop

@IBAction func startRoute(sender: UIButton) {
    // Timer stop() function
    // Each 4 seconds
    timer = NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: #selector(ViewController.stop), userInfo: nil, repeats: true)
}

这是我的stop()函数,它放置一个“起始点”,它将当前位置添加到points类型为var points: [CLLocationCoordinate2D] = []的向量中,并绘制路径addOverlay。

func stop() {
    if (self.initialFlag == 0) {
        // Put flag to 1
        self.initialFlag = 1

        // Add a Annotation
        let sourceAnnotation = MKPointAnnotation()
        sourceAnnotation.title = "Start Point"
        sourceAnnotation.coordinate = (self.locationManager.location?.coordinate)!
        self.mapView.showAnnotations([sourceAnnotation], animated: true)                        
    }

    // Get current point location
    let currentLocation = CLLocationCoordinate2DMake((self.locationManager.location?.coordinate.latitude)!,(self.locationManager.location?.coordinate.longitude)!);

    // We add points every 4 seconds then draw the map points
    self.points.append(currentLocation)


    // Draw the path

    geodesic = MKGeodesicPolyline(coordinates: &points[0], count: points.count)

    // And I have the error in this line below.
    self.mapView.addOverlay(self.geodesic, level: .AboveRoads)

}

1 个答案:

答案 0 :(得分:0)

嘿,看起来如果你没有enought坐标来创建MKGeodesicPolyline它会抛出一个错误,我的解决方案是它

if points.count > 1{
   geodesic = MKGeodesicPolyline(coordinates: &points[0], count: points.count)
   self.mapView.addOverlay(self.geodesic, level: .AboveRoads)
}