In my iPhone app I’m using MapKit to show the user where he is, i also want to show him the total time and duration
to his destination.
Can i use Google direction API to do that?
What I’m planning to do is to ask for direction every few seconds (up to 2500 requests i know..)
and only show the “direction” and “duration” values from the response to the user.
In my iPhone app I’m using MapKit to show the user where he is,
Share
Yes you can.
The JSON response includes distance and duration, e.g.
I would suggest only calling the API every minute or so. You can use linear interpolation to work out the in-between values (your will have to estimate the speed to decrease distance by the right amount).
To implement this….
Frist save the time when the result is returned.
self.responseTime = [NSDate date];Calculate the speed that google used.
Speed = Distance / TimeSo in the example the raw units are meters and seconds so
Speed = 2137146 / 74384 = 28.7... ms^1Use NSTimer to get a recurring callback every so often. In the callback you need:
Decrease time by the elapsed time since the response time.
Decease the distance by the calculated speed * elapsed time since the response time.
Update the labels.