1. Home
  2. Tutorials
  3. Using Native Functionality
  4. Calculate How Far the User Traveled

Calculate How Far the User Traveled

You can calculate how far the user has traveled during a specified period within your app. You will need to set the starting location when an Event occurs in the app, track the location and calculate the distance traveled when another Event occurs.

  1. If you haven’t yet requested location access from the user:
    • If you don’t already have one, use the Page Variables tab on the right of the editor to add a variable to the page you are working in. Enter a name such as “LocationManager” and choose the CLLocationManager type from the Core Location plugin. create location manager
    • Add the Request Location Authorization Action to the Page Loaded Event. In the Action, select the Location Manager variable you created, and an authorization type. request location auth
  2. Add a Page Variable for the starting location, with a name such as “StartLocation”, and CLLocation type. start location var
  3. Decide when you want to set the location for the starting point and add an Action to the relevant Event. Choose the Request Location Update Action, selecting the Location Manager variable you created.
    • In the Location Updated Event, add a Set Value Action.
      • In the first input, select the starting location variable you created.
      • In the second input, select the recentLocation value from the Event Data container. location updated
  4. Decide when you want to calculate the distance, and add the Request Location Update Action to the relevant Event, selecting the Location Manager again.
    • In the Location Updated Event, add the Calculate Distance Action. Select the starting location variable you created, the recent location value from the Event Data, and choose a measurement type. calculate distance updated
    • Add another Action after Calculate Distance to use the returned value from the Action Data container. For example, to display it in the page, add Set Value and set the text value of an Element with the distance number. use distance

If you want to continuously update the distance traveled, use the Start Tracking Location Action. You can also use the Calculate Distance Action with location data returned from an API, or from the user tapping a location on a map. If you have locations returned from an API you can find which one is closest to the user’s location. You can also send location data to the device maps app to show directions, and can calculate travel time to a location.

You can calculate how far the user has traveled during a specified period within your app. You will need to set the starting location when an Event occurs in the app, track the location, and calculate the distance traveled when another Event occurs.

ⓘ Note

To use location functionality on Android you need to set a Google Maps API Key in your project settings.

  1. Add a Page Variable for the starting location, with a name such as “StartLocation”, and LatLng type. create latlng
  2. Decide when you want to set the location for the starting point and add an Action to the relevant Event. Choose the Request Location Update Action, selecting an accuracy level.
    • In the Location Updated Event, add a Set Value Action.
      • In the first input, select the starting location variable you created.
      • In the second input, select the updated LatLng value from the location in the Event Data container. set updated location
  3. Decide when you want to calculate the distance, and add the Request Location Update Action to the relevant Event, selecting an accuracy level again.
    • Add an Action to the Location Updated Event, choosing Calculate Distance. Select the starting location variable, the updated LatLng value from Event Data, and a measurement value. latlng distance
    • Add another Action after Calculate Distance to use the returned value from the Action Data container. For example, to display it in the page, add Set Value and set the text value of an Element with the distance number. latlng set value

If you want to continuously update the distance traveled, use the Start Tracking Location Action. You can also use the Calculate Distance Action with location data returned from an API, or from the user tapping a location on a map. If you have locations returned from an API you can find which one is closest to the user’s location. You can also send location data to the device maps app to show directions, and can calculate travel time to a location.

Was this article helpful to you? Yes No

How can we help?