简体   繁体   中英

Drawing a path with MKMapView problems

I'm trying to draw a path (with MKMapView ). But I have:

NSInvalidArgumentException ', reason:' *** - [NSMutableOrderedSet addObject:]: object can not be nil '.

I have this button action with a NSTimer that calls the function 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)
}

And this is my stop() function, which puts a "Start Point" and it is going adding the current location to the vector called points of type var points: [CLLocationCoordinate2D] = [] and draw the path with the 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)

}

Hey it looks like if you don't have enought coordinates to create MKGeodesicPolyline it throws an error, My solution was it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM