【问题标题】:Swift MapKit not showing directions to pinSwift MapKit 未显示固定方向
【发布时间】:2017-07-28 03:23:01
【问题描述】:

我编写了代码来放置一个图钉,然后在 MapKitView 上显示指向它的方向,由于某种原因,图钉掉了,但方向不会显示在地图上。欢迎大家帮忙,谢谢。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

    @IBOutlet weak var MapView: MKMapView!

    let locationManager = CLLocationManager()
    var movedToUserLocation = false

    func clean() {
        MapView.removeAnnotations(MapView.annotations)
        MapView.removeOverlays(MapView.overlays)
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .denied, .restricted:
            print("Disable Parental Controles")
        case .notDetermined:
            manager.requestWhenInUseAuthorization()
        default:
            manager.startUpdatingLocation()
        }
    }

    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
        if !movedToUserLocation {
            mapView.region.center = mapView.userLocation.coordinate
            movedToUserLocation = true
        }
    }


    func addDirections(coor: CLLocationCoordinate2D) {
        let directionsRequest = MKDirectionsRequest()
        directionsRequest.source = MKMapItem(placemark: MKPlacemark(coordinate: MapView.userLocation.coordinate))
        directionsRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: coor))
        directionsRequest.requestsAlternateRoutes = false
        directionsRequest.transportType = .any

        let directions = MKDirections(request: directionsRequest)
        directions.calculate { response, error in
            if let res = response {
                if let route = res.routes.first {
                    self.MapView.add(route.polyline)
                    self.MapView.region.center = coor
                }
            }
            else {
                print(error)
            }
        }
    }

    @IBAction func B1(_ sender: Any) {
        let lat = 30.2817
        let lon = -86.0188

        self.clean()

        let coord = CLLocationCoordinate2DMake(CLLocationDegrees(lat), CLLocationDegrees(lon))
        let annotationView: MKPinAnnotationView!
        let annotationPoint = MKPointAnnotation()

        annotationPoint.coordinate = coord
        annotationPoint.title = "Pizza By The Sea"

        annotationView = MKPinAnnotationView(annotation: annotationPoint, reuseIdentifier: "Annotation")
        MapView.addAnnotation(annotationView.annotation!)
        addDirections(coor: coord)
    }


    @IBAction func B2(_ sender: Any) {
        let lat = 30.2844
        let lon = -86.0272

        self.clean()

        let coord = CLLocationCoordinate2DMake(CLLocationDegrees(lat), CLLocationDegrees(lon))
        let annotationView: MKPinAnnotationView!
        let annotationPoint = MKPointAnnotation()

        annotationPoint.coordinate = coord
        annotationPoint.title = "George's"

        annotationView = MKPinAnnotationView(annotation: annotationPoint, reuseIdentifier: "Annotation")
        MapView.addAnnotation(annotationView.annotation!)
        addDirections(coor: coord)
    }

    @IBAction func B3(_ sender: Any) {
        let lat = 30.2815
        let lon = -86.0191

        self.clean()

        let coord = CLLocationCoordinate2DMake(CLLocationDegrees(lat), CLLocationDegrees(lon))
        let annotationView: MKPinAnnotationView!
        let annotationPoint = MKPointAnnotation()

        annotationPoint.coordinate = coord
        annotationPoint.title = "Ticheli's Pizza"

        annotationView = MKPinAnnotationView(annotation: annotationPoint, reuseIdentifier: "Annotation")
        MapView.addAnnotation(annotationView.annotation!)
        addDirections(coor: coord)
    }

    @IBAction func B4(_ sender: Any) {
        let lat = 30.2794074
        let lon = -86.0816672

        self.clean()

        let coord = CLLocationCoordinate2DMake(CLLocationDegrees(lat), CLLocationDegrees(lon))
        let annotationView: MKPinAnnotationView!
        let annotationPoint = MKPointAnnotation()

        annotationPoint.coordinate = coord
        annotationPoint.title = "Big Bad Breakfast"

        annotationView = MKPinAnnotationView(annotation: annotationPoint, reuseIdentifier: "Annotation")
        MapView.addAnnotation(annotationView.annotation!)
        addDirections(coor: coord)
    }

    @IBAction func B5(_ sender: Any) {
        let lat = 30.3129063
        let lon = -86.1119704

        self.clean()

        let coord = CLLocationCoordinate2DMake(CLLocationDegrees(lat), CLLocationDegrees(lon))
        let annotationView: MKPinAnnotationView!
        let annotationPoint = MKPointAnnotation()

        annotationPoint.coordinate = coord
        annotationPoint.title = "Cafe Thirty-A"

        annotationView = MKPinAnnotationView(annotation: annotationPoint, reuseIdentifier: "Annotation")
        MapView.addAnnotation(annotationView.annotation!)
        addDirections(coor: coord)
    }

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKPolylineRenderer {
        let renderer = MKPolylineRenderer(overlay: overlay as! MKPolyline)

        renderer.strokeColor = .yellow
        renderer.lineWidth = 5.0

        return renderer
    }

    @objc func dropAnnotation(gestureRecogniser: UIGestureRecognizer) {
        if gestureRecogniser.state == .began {
            let holdLocation = gestureRecogniser.location(in: MapView)
            let coord = MapView.convert(holdLocation, toCoordinateFrom: MapView)

            let annotationView: MKPinAnnotationView!
            let annotationPoint = MKPointAnnotation()

            self.clean()

            annotationPoint.coordinate = coord
            annotationPoint.title = "\(coord.latitude), \(coord.longitude)"

            annotationView = MKPinAnnotationView(annotation: annotationPoint, reuseIdentifier: "Annotation 2")
            MapView.addAnnotation(annotationView.annotation!)
            addDirections(coor: coord)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        MapView.delegate = self
        locationManager.delegate = self

        locationManager.desiredAccuracy = kCLLocationAccuracyBest

        let span = MKCoordinateSpan(latitudeDelta: MapView.region.span.latitudeDelta/200, longitudeDelta: MapView.region.span.longitudeDelta/200)
        let region = MKCoordinateRegion(center: MapView.region.center, span: span)

        let dropPin = UILongPressGestureRecognizer(target: self, action: #selector(self.dropAnnotation(gestureRecogniser:)))
        dropPin.minimumPressDuration = CFTimeInterval(1.0)
        MapView.addGestureRecognizer(dropPin)

        MapView.setRegion(region, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

【问题讨论】:

  • 您确定在从 newViewController 值中选择位置后会传递给您要添加折线的 func 吗?请使用断点检查并打印所选位置坐标和用户当前位置坐标

标签: swift maps mapkit mapkitannotation map-directions


【解决方案1】:

也许MapView.userLocation.coordinatenil

在 Storyboard 上检查 MapView 的“用户位置”。

【讨论】:

  • 我想通了。
【解决方案2】:

mapView(_:rendererFor:) 返回MKOverlayRenderer 对象。

替换

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKPolylineRenderer {

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    相关资源
    最近更新 更多