gpt4 book ai didi

ios - MapKit Direction to Selected 注释

转载 作者:行者123 更新时间:2023-11-28 07:54:44 25 4
gpt4 key购买 nike

试图从用户的当前位置获取到 map 上所选注释的方向,但不确定如何捕获所选注释(当用户选择其中一个时)并显示方向。检查了 Stack 上的不同主题,但没有很多关于如何捕捉选择的注释并显示用户位置方向的新鲜信息。请指教。

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


}

}

最佳答案

要获取 map 上从您的位置到注释的路径,请遵循以下代码:

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

你只需要选择正确的注释,也许可以尝试在注释上注册一个触摸监听器。

关于ios - MapKit Direction to Selected 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48563435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com