Swift - MapKit 未显示固定方向

问题描述 投票:0回答:2

注释已渲染,但是方向叠加层未按预期显示。位置通过 CoreLocation 正确显示。执行文件时不会显示任何错误。

请参阅下面的实现

导入UIKit 导入地图工具包 导入核心位置

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.
}

}

swift maps mapkit mapkitannotation map-directions
2个回答
1
投票

也许

MapView.userLocation.coordinate
nil

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


1
投票

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

更换

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

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
© www.soinside.com 2019 - 2024. All rights reserved.