Ich versuche, einen Pfad (mit MKMapView
) zu zeichnen. Aber ich habe:Einen Pfad mit MKMapView-Problemen zeichnen
NSInvalidArgumentException ', Grund:' *** - [NSMutableOrderedSet addObject:]: Objekt kann nicht Nil 'sein.
Ich habe diese Schaltfläche Aktion mit einem NSTimer
, die die Funktion aufruft 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)
}
Und das ist meine stop()
-Funktion, die einen „Startpunkt“ setzt und es wird die aktuelle Position auf der Zugabe Vektor mit dem Namen points
vom Typ var points: [CLLocationCoordinate2D] = []
und zeichnen Sie den Pfad mit dem 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)
}
Versuchen Kraft Abwickeln optionals loszuwerden und Wache verwenden oder wenn lass die Syntax entpacken. Überprüfen Sie dann, ob locationManager.location.coordinate in dem Moment verfügbar ist, in dem Sie sie aufrufen. Wenn das Problem nicht behoben wird, aktualisieren Sie den Code in der Frage. –
Verwenden Sie den Debugger, um herauszufinden, wo es abstürzt. vielleicht sind Punkte nicht gültig und MKGeodesicPolyline ist dann nicht gültig und MKGeodesicPolyline.points ist null? –
Was ist Punkte [0] und was ist geodesic.points und was ist geodesic.pointCount –