如何绘制路线(没有折线)和没有谷歌API?

时间:2015-10-16 07:48:05

标签: ios swift mapkit

我想在不使用谷歌API的情况下在地图上绘制路线。

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

1 个答案:

答案 0 :(得分:1)

实际上您可以使用MKDirections内置的MapKit Framework。这里有更多信息: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/