ios – 使用MKMapView和MKRoute获取路线问题

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

所以,请问为什么我会在很长的路上得到错误,哪里会是我的问题?

‘Error Domain=MKErrorDomain Code=3 “Directions Not Available”
UserInfo={NSLocalizedFailureReason=Route information is not available
at this moment., MKErrorGEOError=-4, MKDirectionsErrorCode=2,
NSLocalizedDescription=Directions Not Available’

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)
            }
        }
    }
}

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

点赞