未调用MKMapView委托方法-路线未在地图上显示

时间:2019-07-26 00:37:51

标签: ios swift core-location mkdirection

我想显示从一个位置到另一位置的路线。我创建了一些方法。第一个是getDirection(),第二个是createDirectionRequest和rendererFor方法。我在viewDidLoad中调用getDirections方法。但是,当我运行它时,似乎什么也没收到。我在这两种方法中都添加了一条打印语句,但它们未在控制台上显示。它与线程有关。

感谢所有答复和解释

 override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
        locationManager.startUpdatingLocation()
        mapViewOutlet.delegate = self
        getDirections()
        settingImageRating()
func getDirections (){
        guard let location = locationManager.location?.coordinate  else {return}

        let request = createDirectionRequest(from: location)
        let directions = MKDirections(request: request)

        directions.calculate { [unowned self] response, error in
            guard let unwrappedResponse = response else { return }

            for route in unwrappedResponse.routes {
                self.mapViewOutlet.addOverlay(route.polyline)
                self.mapViewOutlet.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
            }
            print("get direction")

        }
    }
 func createDirectionRequest (from coordinate: CLLocationCoordinate2D) -> MKDirections.Request{
        let doubleLatitude = NSDecimalNumber(decimal: (business?.coordinates.latitude)!).doubleValue
        let doubleLongitude = NSDecimalNumber(decimal: (business?.coordinates.longitude)!).doubleValue

        let request = MKDirections.Request()
        request.source =  MKMapItem(placemark: MKPlacemark(coordinate: locationManager.location!.coordinate))
        request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: doubleLatitude, longitude: doubleLongitude), addressDictionary: nil))
        print(request.source!)
        print(request.destination!)
        request.requestsAlternateRoutes = true
        request.transportType = .walking
         print("create direction")

        return request
    }

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
        renderer.strokeColor = UIColor.blue
        return renderer
    }

1 个答案:

答案 0 :(得分:0)

能否请您检查是否在Stroryboard中为MKMapView设置了委托,并在UIViewController文件中设置了委托,例如class ViewController: UIViewController, MKMapViewDelegate

我尝试了这段代码,对我来说很好用。

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate{

    @IBOutlet weak var MKMapVW: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.showRouteOnMap()
    }

    func showRouteOnMap() {

        let coordinateOne = CLLocationCoordinate2D(latitude: CLLocationDegrees(exactly: 40.586746)!, longitude: CLLocationDegrees(exactly: -108.610891)!)
        let coordinateTwo = CLLocationCoordinate2D(latitude: CLLocationDegrees(exactly: 42.564874)!, longitude: CLLocationDegrees(exactly: -102.125547)!)

        let request = MKDirections.Request()
        request.source = MKMapItem(placemark: MKPlacemark(coordinate: coordinateOne, addressDictionary: nil))
        request.destination = MKMapItem(placemark: MKPlacemark(coordinate: coordinateTwo, addressDictionary: nil))
        request.requestsAlternateRoutes = true
        request.transportType = .automobile

        let directions = MKDirections(request: request)

        directions.calculate { [unowned self] response, error in
            guard let unwrappedResponse = response else { return }

            if (unwrappedResponse.routes.count > 0) {
                self.MKMapVW.addOverlay(unwrappedResponse.routes[0].polyline)
                self.MKMapVW.setVisibleMapRect(unwrappedResponse.routes[0].polyline.boundingMapRect, animated: true)
            }
        }
    }

    func mapView(_ mapView: MKMapView!, rendererFor overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKPolyline {
            let polylineRenderer = MKPolylineRenderer(overlay: overlay)
            polylineRenderer.strokeColor = UIColor.blue
            polylineRenderer.lineWidth = 5
            return polylineRenderer
        }
        return nil
    }
}