Luai Kalkatawi
Luai Kalkatawi

Reputation: 1492

Getting directions issue with MKMapView and MKRoute

I'm drawing a line between many points which is start point, way points and end points and put all o them in an array and calculate the route. At the first it is drawing it fine but if I call the same method second time I get the error below and it is happening when I select long route between cities but if I call it many times inside the city(short) it is working fine.

So please why I'm getting the error on long way, where would be my issue?

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

Upvotes: 4

Views: 1658

Answers (1)

gasparuff
gasparuff

Reputation: 2295

This happens because you are sending too many requests in a short period of time. There is a limit of how many requests per second are allowed. Try to skip every second point in your array and see if it works.

Upvotes: 2

Related Questions