1. Home
  2. Tutorials
  3. Using Native Functionality
  4. Calculate Travel Time to a Location

Calculate Travel Time to a Location

travel time

You can calculate the length of time it will take to travel between the user’s location and another location in your app using a range of transportation types. First you will need to detect the user location, then you can calculate the time to travel from it to the target location, which can come from another source in your app, such as API data.

ⓘ Note

On iOS, you will first need to use the Request Location Authorization Action, specifying a Page Variable of type CLLocationManager that you will reference in the Action that calculates travel time. On Android, requesting location permission is handled automatically.

  1. Create a Page Variable for the target location, choosing type CLLocation (iOS) / LatLng Android. Save the target location to the variable, for example using a Create a Location/LatLng Object Action and a Set Value Action. place location place location var
  2. Retrieve the user location with a Request Location Update Action (unless you’re using Start Tracking Location, in which case you can retrieve the location from a CLLocationManager (iOS) / DSLocationTracker Page Variable). location update
  3. When you have an up to date location for the user (e.g. in the nested Location Updated Event of the Request Location Update Action), add the Calculate Travel Time (iOS) / Calculate Distance and Travel Duration (Android) Action. For the Source, select the updated user location – from the Event Data if you’re in a Location Updated Event, or the location tracker/manager Page Variable if you’re tracking the user location continuously. For the Destination, select the target location Page Variable you created. Choose a transportation type. travel time action
    • In the nested success Event, use the Event Data to access the travel time info and use it in your app, like this addition to the Places example app that shows the travel time from the user location to a place search result. show travel time

Your app can also calculate distance and can determine the closest in an array of locations. You can also use the math Action to carry out numeric calculations in your apps.

Was this article helpful to you? Yes No

How can we help?