- 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.
- 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.
- Add a Page Variable for the starting location, with a name such as “StartLocation”, and CLLocation type.
- 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.
- 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.
- 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.
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.
To use location functionality on Android you need to set a Google Maps API Key in your project settings.
- Add a Page Variable for the starting location, with a name such as “StartLocation”, and LatLng type.
- 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.
- 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.
- 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.
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.