MapKit方向选择注释

时间:2018-02-01 13:03:00

标签: ios swift annotations mapkit mapkitannotation

尝试从用户的当前位置获取地图上所选注释的方向但不确定如何捕获选定的注释(当用户选择其中一个时)并显示方向。在堆栈上检查不同的主题但是没有关于如何捕获选定注释并显示用户位置方向的大量新信息。请注意。

import UIKit
import MapKit
import CoreLocation

class NavigationVC: UIViewController {

@IBOutlet weak var mapView: MKMapView!
let manager = CLLocationManager()
let request = MKLocalSearchRequest()

override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
    manager.desiredAccuracy =   kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()


  }


 }
 //MARK: CONFIGURATION OF MAPVIEW

 extension NavigationVC: CLLocationManagerDelegate {

  func locationManager(_ manager: CLLocationManager, didUpdateLocations 
    locations: [CLLocation]) {
    let location = locations[0]
    let span: MKCoordinateSpan = MKCoordinateSpanMake(0.1, 0.1 )
    let userLocation:CLLocationCoordinate2D = 
    CLLocationCoordinate2DMake(location.coordinate.latitude, 
    location.coordinate.longitude)
    let region: MKCoordinateRegion = 
    MKCoordinateRegionMake(userLocation, span)
    mapView.setRegion(region, animated: true)
    self.mapView.showsUserLocation = true

    request.naturalLanguageQuery = "Currency exchange"
    request.region = mapView.region
    let activeSearch = MKLocalSearch(request: request)
    activeSearch.start { (response, error) in

        guard let response = response else {
            return
        }

        for item in response.mapItems {
            let annotation = MKPointAnnotation()
            annotation.coordinate = item.placemark.coordinate
            annotation.title = item.name

            DispatchQueue.main.async {
                self.mapView.addAnnotation(annotation)
            }
        }
    }


  }

  func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> 
 MKOverlayRenderer {
    let polylineRenderer = MKPolylineRenderer(overlay: overlay)
    polylineRenderer.strokeColor = UIColor.blue
    polylineRenderer.fillColor = UIColor.red
    polylineRenderer.lineWidth = 2
    return polylineRenderer


  }

}

2 个答案:

答案 0 :(得分:0)

要从地点到注释获取地图上的路径,请遵循以下代码:

    annotation = MKPointAnnotation()
    annotation.coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat),longitude: CLLocationDegrees(lon))
    self.map?.addAnnotation(annotation)


    var route : MKRoute? = nil

    DispatchQueue.global(qos: .userInitiated).async { [weak self] in
        let directionRequest = MKDirectionsRequest()
        directionRequest.source = MKMapItem.forCurrentLocation();
        directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate:(self?.annotation.coordinate)!))
        directionRequest.transportType = .automobile
        let directions = MKDirections(request: directionRequest)

        directions.calculate {
            (response, error) -> Void in
            guard let response = response else {
                if let error = error {
                    print("Error: \(error)")
                }
                return
            }

            route = response.routes[0]
            if let percorso = route{
                DispatchQueue.main.async { [weak self] in
                    self?.map?.add((percorso.polyline), level: MKOverlayLevel.aboveRoads)
                }
            }
        }
    }

您只需选择正确的注释,也可以尝试在注释上注册触控侦听器。

答案 1 :(得分:0)

使用didSelect委托方法。

e.g。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard let annotation = view.annotation else {
        return
    }

    let directionRequest = MKDirectionsRequest()
    directionRequest.source = MKMapItem.forCurrentLocation()
    directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: annotation.coordinate))
    directionRequest.transportType = .automobile
    let directions = MKDirections(request: directionRequest)

    directions.calculate {
        (response, error) -> Void in
        guard let response = response else {
            if let error = error {
                print("Error: \(error)")
            }
            return
        }

        if !response.routes.isEmpty {
            let route = response.routes[0]
            DispatchQueue.main.async { [weak self] in
                self?.mapView.add(route.polyline)
            }
        }
    }
}

不要忘记guard以防止崩溃。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->
    MKOverlayRenderer {
        guard overlay is MKPolyline else {
            return MKPolylineRenderer()
        }
    ...
}
祝你好运!