Using this template locationTemplate
Closed this issue · 5 comments
Your Environment
- Plugin version: 1.9.1
- Platform: iOS
- OS version: 12.1.4
- Device manufacturer / model: iPhone X
- Nativescript version (
tns info
): 5.2.0 - Plugin config
Expected Behavior
Using this template locationTemplate:
httpRootProperty: 'location',
locationTemplate: '[{"lat":<%= latitude %>,"lng":<%= longitude %>,"heading": <%= heading %>,"speed":<%= speed %> }]',
Actual Behavior
Results in this output {"location":[{"lng":-2.1281124147734172,"lat":52.000321439890222,"speed":-1,"heading":-1},{}]}
The ,{} is not required.
<%=timestamp%> crashes app with error BG Geo Template Error You have an error in your location template.
Steps to Reproduce
add "gpsstamp":<%=timestamp%> to locationTemplate
Context
Debug logs
are you watching the logs and using debug: true
? There's a JSON error being printed to the logs for you to look at.
timestamp
is an ISO-8601 formatted String (eg: "2019-02-28T10:44:28.157Z"
).
You are telling the plugin to generate a json object like this:
{
"timestamp": 2019-02-28T10:44:28.157Z
}
You are responsible for quoting the data
```json
{
"timestamp": "2019-02-28T10:44:28.157Z"
}
It's like this:
{"locationTemplate":'{"timestamp":"<%= timestamp %>"}'
This is the JSON sent to my server now with all the variables. ,{} is appended to the end.
Template is
httpRootProperty: 'data',
locationTemplate: '[{"lat":<%= latitude %>,"lng":<%= longitude %>,"heading": <%= heading %>,"speed":<%= speed %>,"accuracy":<%= accuracy %>,"timestamp":"<%= timestamp %>","event":"<%= event %>","activity_type":"<%= activity.type %>","activity_confidence":<%= activity.confidence %>,"battery_level":<%= battery.level %>,"battery_charging":"<%= battery.is_charging %>" }]',
{"data":[{"battery_charging":"true","speed":-1,"battery_level":1,"accuracy":65,"event":"motionchange","activity_type":"unknown","activity_confidence":100,"lat":52.000290105969263,"timestamp":"2019-03-01T14:44:13.813Z","lng":-2.1282378635378132,"heading":-1},{}]}
Yes, [ ]
templates are problematic. The plugin doesn't know where to append the extras
object so it pushes it onto the Array. You'll have a much better time with { }
templates and modifying your server to consume a different format.
else if ([location isKindOfClass:[NSArray class]]) {
if (_type == TS_LOCATION_TYPE_GEOFENCE && _geofence[@"extras"]) {
if (!_extras) {
_extras = [_geofence[@"extras"] mutableCopy];
} else {
NSMutableDictionary *tmp = [_extras mutableCopy];
[tmp addEntriesFromDictionary:_geofence[@"extras"]];
_extras = [tmp copy];
}
}
if (_extras) {
DDLogWarn(@"Appending #extras to last index of [Array] in JSON template");
[location addObject:_extras]; // <--------------- HERE IS YOUR EXTRA {}
}
}
You should be seeing this warned in the logs: