我正在尝试绘制一条路径(使用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)
}