Swift iOS MapKit及时驾驶距离

时间:2015-07-26 23:51:16

标签: ios objective-c iphone xcode swift

当您在苹果地图中搜索城市等时,它还会显示在指针旁边行驶所需的时间。那是在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)
        }

    }


}

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

提前致谢

1 个答案:

答案 0 :(得分:5)

是的,这是Apple内置的东西!它被称为let options = PHImageRequestOptions() options.networkAccessAllowed = true options.progressHandler = { (progress: Double, _, _, _) -> Void in dispatch_async(dispatch_get_main_queue()) { self.barProgressView.setProgress(Float(progress), animated: true) //FIXME: Can't do this, otherwise it will hide before animation completes self.barProgressView.hidden = (progress <= 0.0 || progress >= 1.0) } } PHImageManager.defaultManager().requestImageForAsset(photoAsset, targetSize: targetSize, contentMode: .AspectFit, options: options) { (result: UIImage?, info: Dictionary?) -> Void in //do something with the image } ,可以给你两点之间的旅行时间。

您可以在预成MKRoute之后创建MKRoute

这里有一个link项目,其中包含使用MKDirectionsRequest的项目,apple documentation以及指向该主题的EVEN MORE有用文档的链接。

祝你好运!