3

I am trying to implement a turn by turn navigation system in iOS using MKDirections API. I am able to get the directions of the route and can draw the polyline on the map, and when navigation starts the app start showing the instructions using the MKRouteStep object in Steps array. Now the problem is, since the distance in the MKRouteStep object is the distance that the user covers while traversing the path of the step I am unable to identify how to calculate this distance. Is there any way to calculate this distance ? Currently I am calculating the distance between two coordinates i.e the coordinate when last instruction was shown with the current location of user and if this distance is equal to the distance mentioned in MKRouteStep I am showing the next instruction, due to which it is delaying the instruction to be delivered to user.

If there is any way to identify the coordinate or region where this MKRouteStep ends I can calculate whether the user is in that region or not and then show the next instruction. May be this can work what are your thoughts, but for this how will I know the coordinate or region where this MKRouteStep ends from polyline array of MKRouteStep?

Please suggest if there is any other good solution for this. Any help is highly appreciated.

Thanks.

Milan Agarwal
  • 427
  • 2
  • 15

1 Answers1

7

Since no one has answered to my question I am answering it myself so that it may help someone looking for the same.

So instead of using distance value of MKRouteStep to calculate when to show next instruction, we can use the last coordinate of polyline array provided by MKRouteStep. For example:

MKMapPoint mapPointForEndOfRouteStep = routeStep.polyline[routeStep.polyline.pointCount - 1];

Now you can create a region around it and check whether your current location lies in this region or not.

Milan Agarwal
  • 427
  • 2
  • 15
  • Hi @Milan, thank you for your post. As you probably know this is not a very hot topic and I am struggling with these directions. May I ask you to post a little bit of your code? For instance, using swift 2 I am not able to create this mapPointForEndOfRouteStep variable. – Quentin Malgaud Sep 18 '15 at 13:33
  • what if the user's position is not exactly on the polygon and is slightly moved from the path, may be around 0.01 meters. In this case i guess the check for current location lies in this region or not will always be false. – Wasim Ahmed Jul 27 '16 at 10:03
  • @milan Did you use region monitoring for this or do you just check yourself (in didUpdateLocations) if the user's current location is within the region? If using region monitoring, how did solve the problem where there could be more than 20 route steps (the system limits to 20 monitored regions). – Aodh Apr 25 '17 at 14:30
  • 1
    @Aodh There is no need to do region monitoring and you can check it yourself by using your current location. – Milan Agarwal Apr 27 '17 at 06:02