1

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

NSInvalidArgumentException ',原因:' *** - [NSMutableOrderedSet addObject:]:对象不能为 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)

}
4

1 回答 1

0

嘿,看起来如果你没有足够的坐标来创建 MKGeodesicPolyline 它会引发错误,我的解决方案就是

if points.count > 1{
   geodesic = MKGeodesicPolyline(coordinates: &points[0], count: points.count)
   self.mapView.addOverlay(self.geodesic, level: .AboveRoads)
} 
于 2016-06-23T19:27:58.540 回答