I have a functionality where user travels by his car from start to end location. I need to calculate the distance between the start and end locations on which the user has travelled. There may be multiple routes to the destination and I am getting these routes from MKDirectionRequest object. But how can I calculate the visited route by driver and its distance whether it may be the shortest or longest route.
Asked
Active
Viewed 221 times
1 Answers
0
When driver location is getting updated then you need to store(on server may be) current location of the driver as he moves with car.
Once driver finishes ride then you need to calculate the distance from latitude & longitude.
so for that you need to add 2 buttons.
Start Ride: So when user start the ride, user press start ride button and then you need to start listing the location updates as driver move with the car and store it on the server.
Stop Ride: So as driver reaches to the destination then user presses the stop ride so here yo need to stop to listen the location updates and calculate the all stored lat. and long.
Hope it may help you.

Mahendra
- 8,448
- 3
- 33
- 56
-
Exactly. I am following the same process of saving latitude and longitude of the travelled path. But while calculating distance, it does not give me the accurate distance if compared with Apple Maps. I am using the MKDirectionRequest method to calculate the distance betweent he two CLLocation objects. – Roshni Jul 27 '20 at 06:12
-
it may possible that `MKDirectionRequest` calculates the distance using the straight line not the path driver have gone through. – Mahendra Jul 27 '20 at 06:22
-
Is there any other way to calculate it? I have a array of lat longs travelled but at last it calculates the distance between the first point and last point in the array which can lead to different distance in case of multiple routes. – Roshni Jul 27 '20 at 06:35
-
its not possible with `MapKit` as of now but you can achieve this using Google's direction api. – Mahendra Oct 15 '20 at 06:23