2016-05-17 96 views
1

我想繪製一條路徑(與MKMapView)。但我有:繪製與MKMapView問題的路徑

NSInvalidArgumentException',原因:'*** - [NSMutableOrderedSet addObject:]:object can not be 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()功能,它把一個「起點」,它會添加當前位置到載體var points: [CLLocationCoordinate2D] = [],類型爲points,並使用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) 

} 
+0

試圖擺脫力展開自選並使用保護或者讓語法解開它。然後檢查您調用locationManager.location.coordinate時是否可用。如果它不能解決問題,請在問題中發佈更新後的代碼。 –

+0

使用調試器找出它崩潰的位置。也許點是無效的,MKGeodesicPolyline是無效的然後MKGeodesicPolyline.points是零? –

+0

什麼是points [0]和whats geodesic.points以及whats geodesic.pointCount –

回答

0

嘿,它看起來像,如果你沒有足夠多的座標以創建MKGeodesicPolyline它拋出一個錯誤,我的解決辦法是

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