gpt4 book ai didi

ios - 显示路线距离

转载 作者:行者123 更新时间:2023-11-28 15:18:51 24 4
gpt4 key购买 nike

首先,我想说我是 Swift 的新手。在我的情况下,最简单的答案可能是最好的。

我目前有一些代码可以使用 mapkit 绘制从我当前位置到大峡谷的路线。开始和结束位置是在代码中构建的:

let soucrceCoordinates = locationManager.location?.coordinate
//coordinates off the grand canyon, placeholder
let destCoordinates = CLLocationCoordinate2DMake(36.1070, -112.1130)

let sourcePlacemark = MKPlacemark(coordinate: soucrceCoordinates!)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)

let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)

let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceItem
directionRequest.destination = destItem
directionRequest.transportType = .automobile

现在我想计算这条路线的距离(而不是乌鸦飞)。最好的单位是米。有没有办法做到这一点?提前致谢

最佳答案

您还必须创建一个 MKDirections 实例,而不仅仅是一个 MKDirectionsRequest 并调用 MKDirections.calculate 来计算导航路线。

let sourceCoordinates = locationManager.location?.coordinate
//coordinates of the grand canyon, placeholder
let destCoordinates = CLLocationCoordinate2D(latitude: 36.1070, longitude: -112.1130)

let sourcePlacemark = MKPlacemark(coordinate: sourceCoordinates!)
let destPlacemark = MKPlacemark(coordinate: destCoordinates)

let sourceItem = MKMapItem(placemark: sourcePlacemark)
let destItem = MKMapItem(placemark: destPlacemark)

let directionRequest = MKDirectionsRequest()
directionRequest.source = sourceItem
directionRequest.destination = destItem
directionRequest.transportType = .automobile

let directions = MKDirections(request: directionRequest)
directions.calculate(completionHandler: { response, error in
guard error == nil, let response = response, let route = response.routes.first else {return}
print("Distance:\(route.distance)meters")
})

关于ios - 显示路线距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46380310/

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