gpt4 book ai didi

ios - 没有得到 "MAP Route"

转载 作者:行者123 更新时间:2023-11-28 06:02:23 25 4
gpt4 key购买 nike

为什么我没有获取“MAP Route”,这段代码有什么问题。或者,请提供“在 map 上获取路线”的代码

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {

let selectedLoc = view.annotation

print("Annotation '\(String(describing: selectedLoc?.title!))' has been selected")

let currentLocMapItem = MKMapItem.forCurrentLocation()

let selectedPlacemark = MKPlacemark(coordinate: (selectedLoc?.coordinate)!, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)

let mapItems = [selectedMapItem, currentLocMapItem]

let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]

MKMapItem.openMaps(with: mapItems, launchOptions:launchOptions)
}

最佳答案

我添加了这个方法来创建 2 个 MKMapItem

之间的 map 路线
func createRouteTo(fromMapItem: MKMapItem,selectedMapItem: MKMapItem) {

if(CLLocationCoordinate2DIsValid(self.mapview.userLocation.coordinate) && CLLocationCoordinate2DIsValid(selectedMapItem.placemark.coordinate))
{
self.showLoading(message: "Creando Ruta...")
let directionRequest = MKDirectionsRequest()
directionRequest.source = MKMapItem(placemark: MKPlacemark(coordinate: fromMapItem.placemark.coordinate, addressDictionary: nil))
directionRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: selectedMapItem.placemark.coordinate, addressDictionary: nil))
directionRequest.transportType = .automobile
directionRequest.requestsAlternateRoutes = false

// Calculate the direction
let directions = MKDirections(request: directionRequest)

directions.calculate { [unowned self] (directionResponse, error) in
self.hideLoading(withDelay: 0.1)
guard let response = directionResponse else {
if let error = error {
print("Error: \(error)")
let alert = UIAlertController.init(title:"Error Creando Ruta",
message:error.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Aceptar", style: .destructive, handler:nil))
self.present(alert, animated:true , completion:nil)
}

return
}

if(self.currentRouteOverlay != nil)
{
self.mapview.remove(self.currentRouteOverlay!)
}

let route = response.routes[0]
self.currentRouteOverlay = route.polyline
self.mapview.add(route.polyline, level: .aboveRoads)

let rect = route.polyline.boundingMapRect
self.mapview.setVisibleMapRect(rect, edgePadding: UIEdgeInsetsMake(15, 15, 15, 15), animated: true)
}
}

}

这是你修改的方法

// This method will be called when we will tap on the annotation for Details.Then it will open in new View and make a route from current location to selected location.
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {

let selectedLoc = view.annotation

print("Annotation '\(String(describing: selectedLoc?.title!))' has been selected")

let currentLocMapItem = MKMapItem(placemark: MKPlacemark(coordinate: mapView.userLocation.coordinate))

let selectedPlacemark = MKPlacemark(coordinate: (selectedLoc?.coordinate)!, addressDictionary: nil)
let selectedMapItem = MKMapItem(placemark: selectedPlacemark)

self.createRouteTo(fromMapItem:currentLocMapItem, selectedMapItem: selectedMapItem)
}

完整代码在这里 https://github.com/rmelian2014/MapRouteQuestion

enter image description here

关于ios - 没有得到 "MAP Route",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49228863/

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