locationTemplate property

String? locationTemplate
final

Optional custom template for rendering location JSON request data in HTTP requests.

The locationTemplate will be evaluated for variables using Ruby erb-style tags:

<%= variable_name %>

See also:

BackgroundGeolocation.ready(Config(
  persistence: PersistenceConfig(
    locationTemplate: '{"lat":<%= latitude %>,"lng":<%= longitude %>,"event":"<%= event %>",isMoving:<%= isMoving %>}',
  )
));

// Or use a compact [Array] template!
  persistence: PersistenceConfig(
    locationTemplate: '[<%=latitude%>, <%=longitude%>, "<%=event%>", <%=is_moving%>]'
  ),
));

Warning: quoting String data.

The SDK does not automatically apply double-quotes around String data. The SDK will attempt to JSON encode your template exactly as you're configured.

The following will generate an error:

BackgroundGeolocation.ready(Config(
  persistence: PersistenceConfig(
    locationTemplate: '{"timestamp": <%= timestamp %>}'
  )
));

Since the template-tag timestamp renders a string, the rendered String will look like this:

{"timestamp": 2018-01-01T12:01:01.123Z}

The correct locationTemplate is:

BackgroundGeolocation.ready(Config(
  persistence: PersistenceConfig(
    locationTemplate: '{"timestamp": "<%= timestamp %>"}'
  )
));
{"timestamp": "2018-01-01T12:01:01.123Z"}

Configured PersistenceConfig.extras:

If you've configured PersistenceConfig.extras, these key-value pairs will be merged directly onto your location data. For example:

BackgroundGeolocation.ready(Config(
  http: HttpConfig(
    rRootProperty: 'data'
  ),
  persistence: PersistenceConfig(
    locationTemplate: '{"lat":<%= latitude %>,"lng":<%= longitude %>}',
    extras: {
      "foo": "bar"
    }
  )
))

Will result in JSON:

{
    "data": {
        "lat":23.23232323,
        "lng":37.37373737,
        "foo":"bar"
    }
}

Template Tags

Tag Type Description
latitude Float
longitude Float
speed Float Meters
heading Float Degrees
accuracy Float Meters
altitude Float Meters
altitude_accuracy Float Meters
timestamp String ISO-8601
uuid String Unique ID
event String motionchange,geofence,heartbeat,providerchange
odometer Float Meters
odometer_error 'Float` Meters
activity.type String still,on_foot,running,on_bicycle,in_vehicle,unknown
activity.confidence Integer 0-100%
battery.level Float 0-100%
battery.is_charging Boolean Is device plugged in?
mock Boolean true when location was recorded from a Mock location app.
is_moving Boolean true when location was recorded while SDK was in moving state.
timestampMeta Object Renders timestamp meta-data. See GeoConfig.enableTimestampMeta.

Implementation

final String? locationTemplate;