Workout, a Tizen Sample App: Calculating Distance Traveled
Patryk Falba
Senior Software Engineer
This is the second blog in a series to introduce the sample application Workout, a Tizen example for monitoring health sensors on a wearable device.
The previous blog, Workout -- A Tizen Sample App for Monitoring Health Sensors, introduced the sample application, Workout, for runners who own a wearable device. In this blog, I will describe how one of the key features, traveled distance, is calculated.
Implementation
To calculate the traveled distance, the application uses the LocationService
class providing location-related GPS data. This service uses the Tizen.Location API to initialize the GPS receiver:
Services/LocationService.cs
/// <summary>
/// Initializes LocationService class instance.
/// </summary>
private LocationService()
{
_locator = new Locator(LocationType.Hybrid)
{
Interval = _gpsCallbackInterval
};
AttachEvents();
}
The API is also used to set the change handlers:
Services/LocationService.cs
/// <summary>
/// Sets service listeners.
/// </summary>
private void AttachEvents()
{
_locator.ServiceStateChanged += (sender, args) => ServiceStateChanged?.Invoke(this, args.ServiceState);
_locator.LocationChanged += (sender, args) => LocationChanged?.Invoke(this, args.Location);
_locator.SettingChanged += (sender, args) => SettingChanged?.Invoke(this, args.IsEnabled);
}
Every time the location changes, the LocationChanged
event is invoked with the new location.
This event has an attached listener in LocationModel
which receives the new location object. The new location is used to calculate the distance to the previous location and stored in a _locationData
object:
Models/LocationModel.cs
_locationData.Distance += location.GetDistanceTo(_lastLocation) / SettingsService.Instance.Distance.UnitToKmRatio;
The new location data is passed to MainModel
, where all workout data are gathered and processed before being sent to viewModels
.
The entire flow of location data and other workout data is described in detail at tizenschool.org
In the next blog in this series, I will discuss how data is gathered from the heart rate monitor.