2017-03-02 2 views
1

Ich versuche, eine Anwendung zu erstellen, eine seiner Funktionen ist das Zeichnen der Linie, während die Benutzer sich bewegen.Warum wurde die MKPolyline nicht in meiner Anwendung angezeigt?

Hier ist die Klasse

class traceuserViewController: UIViewController,CLLocationManagerDelegate, MKMapViewDelegate { 
     var locationManager = CLLocationManager() 
     var startLocation: CLLocation? 
     var endLocation: CLLocation? 
     @IBOutlet weak var mapView: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     self.locationManager.distanceFilter = 30.0 
     self.locationManager.startMonitoringSignificantLocationChanges() 
     self.locationManager.startUpdatingLocation() 
     mapView.showsUserLocation = true 
     mapView.mapType = .hybrid 
     self.mapView.delegate = self 
} 
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    //user's current location 
    let nowlocation = locations.last 
    userLocations.append(nowlocation!) 

    print("HERE IS THE LOCATION ARRAY") 
    print(userLocations) 

    //show the current location region 
    let center = CLLocationCoordinate2D(latitude: nowlocation!.coordinate.latitude, longitude: nowlocation!.coordinate.longitude) 
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.7, longitudeDelta: 0.7)) 
    self.mapView.setRegion(region, animated: true) 
    drawRoute(locationArray: userLocations) 
} 
    func drawRoute(locationArray: [CLLocation]) { 
    if (locationArray.count) > 1 { 
     var destinationLocIndex = (locationArray.count) - 1 
     var startLocIndex = (locationArray.count) - 2 

     let destinationloc = locationArray[destinationLocIndex].coordinate 
     let startLoc = locationArray[startLocIndex].coordinate 

     var routeArray = [startLoc, destinationloc] 
     //test if the function works well or not 
     print(routeArray) 
     var geodesicLine = MKGeodesicPolyline(coordinates: routeArray , count: routeArray.count) 
     mapView.add(geodesicLine, level: .aboveRoads) 

    } 
} 

//draw in the mapview 
private func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer! { 
    if overlay is MKPolyline{ 
     let polylineRenderer = MKPolylineRenderer(overlay: overlay) 
     polylineRenderer.strokeColor = UIColor.blue 
     polylineRenderer.lineWidth = 5.0 
     return polylineRenderer 
    }else{ 
     os_log("Failed to draw the polyline", log: OSLog.default, type: .debug) 
     return nil 
    } 
} 

Nach viele Male versucht, ich habe noch keine Ahnung, warum es nicht die Route auf der Karte nicht ziehen, wenn der Benutzer sich bewegt, kann jemand bitte ich mir habe einige Hinweise?

prost

Antwort

0

Ich Folgern, dass Sie Swift 3 aus dem Code-Schnipsel (zum Beispiel die Unterschrift von didUpdateLocations, die Verwendung von .hybrid statt Swift 2.3 des .Hybrid, etc.) verwenden.

Aber die Unterschrift für mapView(_:rendererFor:) falsch. In Swift 3 ist es:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    ... 
} 

Wenn Sie jemals eine delegierte Methode haben, die nicht zu funktionieren scheinen, einen Haltepunkt in ihm hinzufügen und Sie können bestätigen, ob es überhaupt genannt wird oder nicht (und wenn es genannt, können Sie hindurchgehen und das Problem weiter diagnostizieren).

+0

Ja, es funktioniert jetzt! Vielen Dank: P – Jenny

Verwandte Themen