MapKit Направление к выбранной аннотации
Попытка получить направление к выбранной аннотации на карте из текущего местоположения пользователя, но не уверена, как поймать выбранную аннотацию (когда пользователь выбирает одну из них) и показать направление. Проверены различные темы в стеке, но не так много Свежая информация о том, как поймать выбранную аннотацию и показать направление для местоположения пользователя. Пожалуйста, сообщите.
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 ответа
Использование didSelect
метод делегата.
например
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()
}
...
}
Удачи!
Чтобы получить путь на карте от вашей позиции к аннотации, следуйте этому коду:
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)
}
}
}
}
Вам нужно только выбрать правильную аннотацию, возможно, попробуйте зарегистрировать сенсорный прослушиватель на аннотацию.