Рисование пути с проблемами MKMapView

Я пытаюсь нарисовать путь (с 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)

}

1 ответ

Эй, похоже, что если у вас нет достаточно координат для создания MKGeodesicPolyline, он выдаст ошибку, Мое решение было

if points.count > 1{
   geodesic = MKGeodesicPolyline(coordinates: &points[0], count: points.count)
   self.mapView.addOverlay(self.geodesic, level: .AboveRoads)
} 
Другие вопросы по тегам