How to check if a given coordinate lies on a route or not using Swift?

260 views Asked by At

I am working on a project using MapKit where i need to find that either a given CLLocationCoordinate2D falls on a MKroute or not ?

I tried getting all the coordinates of the route using getCoordinates() method to check each one's distance with the given coordinate but this doesn't work since the method doesn't provide all coordinates.

Please suggest some approach, it doesn't need to be optimal. It should just solve the problem.

2

There are 2 answers

0
LOV LAMBA On

Till now the best solution i found is to check the distance of the given coordinate with each line segment.

By using route.polyline.getCoordinates() method, We already have an array of coordinates that makes the route.

Now all we have to do is -

  1. to loop over the array & take two adjacent coordinates at every iteration ( array[count] & array[count + 1] )
  2. Make a segment of them.
  3. Check the distance of the given coordinate with this segment.
  4. If the distance < given value ( meters upto which we want to check the coordinate ), our coordinate is in the range.
0
Rajasekhar Pasupuleti On

I suggest to use Turf library to handle this kind of logics