获取MKMapView和MKRoute

时间:2016-01-19 12:14:59

标签: ios swift mkmapview mapkit

我在许多点之间绘制一条线,即起点,路点和终点,并将它们全部放在一个数组中并计算路线。在第一次它正在绘制它,但如果我第二次调用相同的方法我得到下面的错误,当我在城市之间选择长途路线但是如果我在城市内多次呼叫它(短)它正在工作正常。

那么请问为什么我会在很长一段时间内收到错误,哪里会是我的问题?

  

'错误域= MKErrorDomain代码= 3“方向不可用”   UserInfo = {NSLocalizedFailureReason =路由信息不可用   此时。,MKErrorGEOError = -4,MKDirectionsErrorCode = 2,   NSLocalizedDescription =方向不可用'

func calculateRoute(wayPoint: [MKMapItem]) {

    let request:MKDirectionsRequest = MKDirectionsRequest()
    var allPointsArray: [MKMapItem] = []
    var directions: MKDirections = MKDirections(request: request)
    var directionsResponse: MKDirectionsResponse = MKDirectionsResponse()
    var route: MKRoute = MKRoute()

    for points in wayPoint{
        allPointsArray.append(points)
    }

    for var i = 0; i < allPointsArray.count - 1; ++i {

        request.source = allPointsArray[i]
        request.destination = allPointsArray[i+1]
        request.transportType = MKDirectionsTransportType.Automobile
        request.requestsAlternateRoutes = false

        directions = MKDirections(request: request)
        directions.calculateDirectionsWithCompletionHandler { (response: MKDirectionsResponse?, error: NSError?) -> Void in

            if error == nil {

                directionsResponse = response!
                route = directionsResponse.routes[0]
                self.mapView.addOverlay(route.polyline, level: MKOverlayLevel.AboveRoads)

            }else {
                print(error)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这是因为您在短时间内发送了太多请求。每秒允许的请求数量有限制。尝试跳过数组中的每个第二点,看看它是否有效。