MKDirections API - Routenlinie wird nicht angezeigt

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • MKDirections API - Routenlinie wird nicht angezeigt

    Liebe Community,

    wer kann mir bei folgendem Problem helfen: Ich habe eine destination location und eine location, bei der sich der user befindet. Beide werden in meiner app angezeigt. Ich habe nun den code geschrieben, der die Route zwischen beiden Punkten berechnen soll und eine blaue Line über den Weg legen soll. Das Programm kompiliert, aber die Route wird nicht angezeigt. Bin wirklich sehr dankbar für Hilfe, da ich schon eine Weile über dem Problem sitze. Vielen Dank!!

    Quellcode

    1. import UIKit
    2. import MapKit
    3. class MapViewController: UIViewController, MKMapViewDelegate
    4. {
    5. //outlet variable is used for establishing a connection with the map view in the storyboard
    6. @IBOutlet var mapView: MKMapView!
    7. var spot = Spot()
    8. let locationManager = CLLocationManager()
    9. var currentPlacemark:CLPlacemark?// it is used to save the selected spot
    10. override func viewDidLoad()
    11. {
    12. super.viewDidLoad()
    13. //request for a user's authorization for lacation services
    14. locationManager.requestWhenInUseAuthorization()
    15. let status = CLLocationManager.authorizationStatus()
    16. if status == CLAuthorizationStatus.authorizedWhenInUse
    17. {
    18. mapView.showsUserLocation = true
    19. }
    20. let geoCoder = CLGeocoder()
    21. geoCoder.geocodeAddressString(spot.location,
    22. completionHandler:
    23. { placemarks, error in
    24. if let error = error {
    25. print(error)
    26. return
    27. }
    28. if let placemarks = placemarks{
    29. //get the first placemark
    30. let placemark = placemarks[0]
    31. // value of current Placemark
    32. self.currentPlacemark = placemark
    33. // add annotation
    34. let annotation = MKPointAnnotation()
    35. annotation.title = self.spot.name
    36. annotation.subtitle = self.spot.type
    37. if let location = placemark.location{
    38. annotation.coordinate = location.coordinate
    39. //display the annotation
    40. self.mapView.showAnnotations([annotation],animated:true)
    41. self.mapView.selectAnnotation(annotation, animated: true)
    42. }
    43. }
    44. })
    45. // mapView.showsCompass = true
    46. // mapView.showsTraffic = true
    47. // mapView.showsScale = true
    48. // we want to show the users location
    49. mapView.showsUserLocation = true
    50. }
    51. @IBAction func showDirection(sender:AnyObject)
    52. {
    53. // we make sure if current placemark contains a value using a guard statement. Otherwise just
    54. // skip everything
    55. guard let currentPlacemark = currentPlacemark else
    56. {
    57. return
    58. }
    59. // creating an instance of MKDirectionsRequest to request directions
    60. let directionRequest = MKDirectionsRequest()
    61. // set the source(where the user currently is) and destination of the route
    62. directionRequest.source = MKMapItem.forCurrentLocation()// retrieving the current location
    63. let destinationPlacemark = MKPlacemark(placemark: currentPlacemark)
    64. directionRequest.destination = MKMapItem(placemark: destinationPlacemark)// end point of the route
    65. directionRequest.transportType = MKDirectionsTransportType.automobile// later change for transit
    66. // calculate the direction
    67. let directions = MKDirections(request: directionRequest)
    68. // this method initiates an asynchronous request for directions and calls
    69. // your completion handler when the request is conpleted. The MKDirections object
    70. //passes my request to the Apple servers and asks for route-based directions data
    71. directions.calculate { (routeResponse, routeError) -> Void in
    72. guard let routeResponse = routeResponse else
    73. {
    74. if let routeError = routeError
    75. {
    76. print("Error:\(routeError)")
    77. }
    78. return
    79. }
    80. let route = routeResponse.routes[0]// provides a container for saving the route information so that the routes are saved in the routes property
    81. // The detailed route geometry e.g. route.polyline is represented by an MKPolyline object
    82. // the add level method is used to add an MKPolyline object to the existing map view
    83. //self.mapView.add((route?.polyline)!,level: MKOverlayLevel.aboveRoads)
    84. self.mapView.add(route.polyline,level: MKOverlayLevel.aboveRoads)
    85. }
    86. }
    87. // implementing a mapView method which draws the route
    88. func mapView(_ mapView:MKMapView,rendererFor overlay: MKOverlay) -> MKOverlayRenderer
    89. {
    90. let renderer = MKPolylineRenderer(overlay: overlay)
    91. renderer.strokeColor = UIColor.blue
    92. renderer.lineWidth = 3.0
    93. return renderer
    94. }
    95. override func didReceiveMemoryWarning()
    96. {
    97. super.didReceiveMemoryWarning()
    98. // Dispose of any resources hat can be recreated.
    99. }
    100. }
    Alles anzeigen