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!!
Alles anzeigen
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
- import UIKit
- import MapKit
- class MapViewController: UIViewController, MKMapViewDelegate
- {
- //outlet variable is used for establishing a connection with the map view in the storyboard
- @IBOutlet var mapView: MKMapView!
- var spot = Spot()
- let locationManager = CLLocationManager()
- var currentPlacemark:CLPlacemark?// it is used to save the selected spot
- override func viewDidLoad()
- {
- super.viewDidLoad()
- //request for a user's authorization for lacation services
- locationManager.requestWhenInUseAuthorization()
- let status = CLLocationManager.authorizationStatus()
- if status == CLAuthorizationStatus.authorizedWhenInUse
- {
- mapView.showsUserLocation = true
- }
- let geoCoder = CLGeocoder()
- geoCoder.geocodeAddressString(spot.location,
- completionHandler:
- { placemarks, error in
- if let error = error {
- print(error)
- return
- }
- if let placemarks = placemarks{
- //get the first placemark
- let placemark = placemarks[0]
- // value of current Placemark
- self.currentPlacemark = placemark
- // add annotation
- let annotation = MKPointAnnotation()
- annotation.title = self.spot.name
- annotation.subtitle = self.spot.type
- if let location = placemark.location{
- annotation.coordinate = location.coordinate
- //display the annotation
- self.mapView.showAnnotations([annotation],animated:true)
- self.mapView.selectAnnotation(annotation, animated: true)
- }
- }
- })
- // mapView.showsCompass = true
- // mapView.showsTraffic = true
- // mapView.showsScale = true
- // we want to show the users location
- mapView.showsUserLocation = true
- }
- @IBAction func showDirection(sender:AnyObject)
- {
- // we make sure if current placemark contains a value using a guard statement. Otherwise just
- // skip everything
- guard let currentPlacemark = currentPlacemark else
- {
- return
- }
- // creating an instance of MKDirectionsRequest to request directions
- let directionRequest = MKDirectionsRequest()
- // set the source(where the user currently is) and destination of the route
- directionRequest.source = MKMapItem.forCurrentLocation()// retrieving the current location
- let destinationPlacemark = MKPlacemark(placemark: currentPlacemark)
- directionRequest.destination = MKMapItem(placemark: destinationPlacemark)// end point of the route
- directionRequest.transportType = MKDirectionsTransportType.automobile// later change for transit
- // calculate the direction
- let directions = MKDirections(request: directionRequest)
- // this method initiates an asynchronous request for directions and calls
- // your completion handler when the request is conpleted. The MKDirections object
- //passes my request to the Apple servers and asks for route-based directions data
- directions.calculate { (routeResponse, routeError) -> Void in
- guard let routeResponse = routeResponse else
- {
- if let routeError = routeError
- {
- print("Error:\(routeError)")
- }
- return
- }
- let route = routeResponse.routes[0]// provides a container for saving the route information so that the routes are saved in the routes property
- // The detailed route geometry e.g. route.polyline is represented by an MKPolyline object
- // the add level method is used to add an MKPolyline object to the existing map view
- //self.mapView.add((route?.polyline)!,level: MKOverlayLevel.aboveRoads)
- self.mapView.add(route.polyline,level: MKOverlayLevel.aboveRoads)
- }
- }
- // implementing a mapView method which draws the route
- func mapView(_ mapView:MKMapView,rendererFor overlay: MKOverlay) -> MKOverlayRenderer
- {
- let renderer = MKPolylineRenderer(overlay: overlay)
- renderer.strokeColor = UIColor.blue
- renderer.lineWidth = 3.0
- return renderer
- }
- override func didReceiveMemoryWarning()
- {
- super.didReceiveMemoryWarning()
- // Dispose of any resources hat can be recreated.
- }
- }