How do I make a multitude of driving distance calculations in MapKit?

How do I make a multitude of driving distance calculations in MapKit using MKDirections without getting a loading throttled error? Is there a different way of making the calculations other than by using MKDirections?

Do you perform a MKDirectionsRequest each time a new character is added to your search string ?
If so, you can receive MKErrorLoadingThrottled error quite easily.

You should delay your request using a timer each time a new character is added, or use an API dedicated to autocompletion.

I suggest you to take a look at :

I haven’t written any code yet. I want to write an app that takes a list of addresses of visitors to visit a list of addresses of hosts and assigns the visitors to the hosts that are nearest to them. That means I would need to calculate the driving distance of each visitor to each host. That would take a lot of calculations. I can foresee easily getting a loading throttled error.

How should I approach this problem?

You can make some pre-filtering before doing all these calculations, using the coordinate of each hosts.

For example, if you find the nearest host using CLLocation's distanceFromLocation method. You will find the nearest host, using a flying distance and not a driving distance.
Now you can make an assumption : the driving distance is someway close to the flying distance, using a small multiplier value (x1.5, x2 ? you should do some tests in order to find which coefficient use here).
Next you search all the hosts included within a circle with a radius equal to your multiplier factor multiplied by the smallest flying distance : you can bet your the nearest one (using driving distance) will be among these ones most of the times.
For the rares cases where the nearest is not in this set, you will anyway select a near host anyway.

This is just the first idea that popped in my head, but I’m pretty sure there are others way to pre-filter the set of data in order to reduce the number of driving distance you’ll have to find.

1 Like

What do you mean by that statement? I don’t understand.

This sentence was indeed quite unclear ^^
Let’s illustrate it with a simple example :

Consider you find a nearest host by flying distance at exactly 10km, but by driving you’ll need 30km to reach him, due to some obscure road planning or geographical barrier (example : someone living on the other side of a mountain, with no tunnel).

Using the pre-filter algorithm, you will be looking for all the hosts included within a 20km radius circle (if you use an arbitrary 2x multiplier value).

There are chances you’ll find a new host in this area with a driving distance smaller than the 30km needed to reach the flying nearest host, and you will thus use this one as the driving nearest.

Still, you can’t be sure it will be the absolute nearest : maybe you’ll reach this driving nearest with only 25km, but you can’t be sure there is not a host at 21km flying distance (so, outside of the circle) than can be reach with 21km in car (yep, a 21km-long straight road).

But by writing this post, I realized you can calculate your circle radius in order to BE SURE to include the driving nearest ! Instead of using an arbitrary factor to compute the circle radius, you can instead use the driving distance to the flying nearest one. In our example, the circle will include all the hosts included within a 30km radius, and thus you can’t miss the real driving nearest one :wink:

This topic was automatically closed after 166 days. New replies are no longer allowed.