gpt4 book ai didi

ios - Swift iOS MapKit 及时行驶距离

转载 作者:搜寻专家 更新时间:2023-10-31 22:15:11 25 4
gpt4 key购买 nike

当您在苹果 map 中搜索城市等时,它还会在指针旁边显示开车到那里所需的时间。这是 mapKit API 中内置的东西吗?

到目前为止我的代码是:

import UIKit
import MapKit

class ShowMapViewController: UIViewController {

var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!

//Map
@IBOutlet weak var mapView: MKMapView!

//Exitbutton

@IBAction func exitMapButtonDidTouch(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}


override func viewDidLoad() {
super.viewDidLoad()

//TODO: Search zip, if not found search city
localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = "My city"
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

//Show city if zip not found
if localSearchResponse == nil{
var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
alert.show()
return
}

//Show map and pointer
self.pointAnnotation = MKPointAnnotation()
//self.pointAnnotation.title = "My city"
self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse.boundingRegion.center.latitude, longitude: localSearchResponse.boundingRegion.center.longitude)


self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
self.mapView.centerCoordinate = self.pointAnnotation.coordinate
self.mapView.addAnnotation(self.pinAnnotationView.annotation)
}

}


}

有了这个,我可以在指针上方添加​​一个标题,但是否也可以显示时间/距离?

提前致谢

最佳答案

是的,这是 Apple 内置的东西!它称为 MKRoute,可以为您提供两点之间的旅行时间。

您可以在执行 MKDirectionsRequest 之后创建一个 MKRoute

这是一个 link对于使用 MKDirectionsRequest 的项目,到 apple documentation ,以及指向 EVEN MORE 的链接有关该主题的有用文档。

祝你好运!

关于ios - Swift iOS MapKit 及时行驶距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31643166/

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