ios - 如何在没有 google api 的情况下绘制路线(没有折线)?

标签 ios swift mapkit

我想在 map 上绘制路线而不使用 google API。

我正在使用用户坐标跟踪用户。所以我画折线来显示用户路线,但现在我的客户想要删除这些折线并想要显示像谷歌那样的路线。我是否可以传递在这些坐标上绘制的所有坐标路线图?

最佳答案

实际上,您可以使用 MKDirections 内置的 MapKit 框架。这是更多信息:https://developer.apple.com/library/mac/documentation/MapKit/Reference/MKDirections_class/ .

向您展示一些正在计算两个位置的路线的片段:

func caculateDirections(fromCoordinate: CLLocationCoordinate2D, toCoordinate: CLLocationCoordinate2D) {
        let from = MKMapItem(placemark: MKPlacemark(coordinate: fromCoordinate, addressDictionary: nil))
        let to = MKMapItem(placemark: MKPlacemark(coordinate: toCoordinate, addressDictionary: nil))

        let request = MKDirectionsRequest()
        request.source = from
        request.destination = to
        request.transportType = .Automobile

        let directions = MKDirections(request: request)
        directions.calculateDirectionsWithCompletionHandler { (response, error) -> Void in
            if let response = response {
                // Here you can get the routes and draw it on the map
                let routes = response.routes
            } else {
                print("Error:\(error?.description)")
            }
        }
    }

您可以从本教程中了解更多信息:http://www.devfright.com/mkdirections-tutorial/

关于ios - 如何在没有 google api 的情况下绘制路线(没有折线)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33165158/

相关文章:

ios - 如何覆盖 JSQMessagesViewController 中自定义媒体项的 mediaView 显示大小?

ios - 由于 http post 请求上的 '&' 字符,字符串参数被分割

swift - 跳过选择,直接在 MapKit 标记中拖动

ios - 如何在 WidgetKit、SwiftUI 中更改午夜 View ?

ios - 在 swift 4 中比较 Day from Date 的问题

ios - 如何使用 swift 2.2 让警报 View 出现在 View Controller 的中心

ios - 如何水平居中自动调整 UICollectionView 单元格?

swift2 - 使用 Swift 2 进行 map 聚类

ios - 在没有互联网连接的情况下在 iOS 中使用 MapView

ios - 选择 UITableView 时使内容可见