почему я не могу отобразить синюю линию между двумя местоположениями?быстрый - PullRequest
0 голосов
/ 16 марта 2019

Я новичок в Свифте.Я пытаюсь соединить две локации синей линией, но эта линия никогда не появляется.Расположение булавок отображает одну карту.import UIKit import MapKit

класс customPin: NSObject, MKAnnotation {координата var: CLLocationCoordinate2D var title: String?var subtitle: String?

init( pinTitle:String, pinSubTitle:String, location:CLLocationCoordinate2D){
    self.title = pinTitle
    self.subtitle = pinSubTitle
    self.coordinate = location
}

} class ViewController класса: UIViewController, MKMapViewDelegate {

@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let sourceLocation = CLLocationCoordinate2D(latitude: 39.173209, longitude: -73.9772)
    let destinationLocation = CLLocationCoordinate2D(latitude: 40.7527, longitude: -90.177429)
    let sourcePin = customPin(pinTitle: "Kansas City", pinSubTitle: "", location: sourceLocation)
    let destinationPin = customPin(pinTitle: "St. Louis", pinSubTitle: "", location: destinationLocation)
    self.mapView.addAnnotation(sourcePin)
    self.mapView.addAnnotation(destinationPin)

    let sourcePlaceMark = MKPlacemark(coordinate: sourceLocation)
    let destinationPlaceMark = MKPlacemark(coordinate: destinationLocation)

    let directionRequest = MKDirectionsRequest()

    directionRequest.source = MKMapItem(placemark: sourcePlaceMark)
    directionRequest.destination = MKMapItem(placemark: destinationPlaceMark)
    directionRequest.transportType = .automobile


    let directions = MKDirections(request: directionRequest)
    directions.calculate{(response, error) in
        guard let directionResonse = response else{
            if let error = error {
                print("we have error getting directions ==\(error.localizedDescription)")
            }
            return
        }
        let route = directionResonse.routes[0]
        self.mapView.add(route.polyline, level: .aboveRoads)

        let rect = route.polyline.boundingMapRect
        self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
    }
    self.mapView.delegate = self
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.blue
    renderer.lineWidth = 4.0
    return renderer
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...