Tracking the current location of devices
You can track the location of devices by ensuring that ongoing acquisition of geo-locational data is taking place. When the position of the device changes, a trigger is activated.
About this task
You acquire geo-locational data from a device by using the WL.Device.startAcquisition API. The PositionChange trigger is activated if the position of the device changes significantly, and events can then be sent to the server. The server handles these events by setting up an event handler.
For example, a warehouse could improve the efficiency of its processes by using locational data from its delivery vehicles to guide the vehicles to the correct docks, and notify warehouse personnel so that they can be prepared for the arrival of the vehicles.
Procedure
- The acquisition of geo-locational data is initiated by the WL.Device.startAcquisition API.
- The PositionChange trigger in the API is used to emit events that are then transmitted to the server. For "live" views, either the transmission interval that is set in the WL.Client.setEventTransmissionPolicy API should be small, or the transmitImmediately parameter must be set to true.
- An event handler is set up on the server by using the WL.Server.createEventHandler(filter,handlerFunction) API. The filter is a literal object that is used to match only the events that you want the handler function to handle.
- The events that are transmitted to the server contain the client's device context at the time the trigger was activated. The handler can pass this, or other information, to external systems where, for example, the data could be displayed on a map.
Example
- Adapter code
-
function handleDeviceLocationChange(event) { // do something with event } function handleDeliveryTruckMoved(event) { // do something with event } function handleRefrigeratedDeliveryTruckMoved(event) { // do something with event } var deviceMoveHandler = WL.Server.createEventHandler( {}, handleDeviceLocationChange ); var deliveryTruckMovedHandler = WL.Server.createEventHandler( {vehicle: "DeliveryTruck"}, handleDeliveryTruckMoved ); var coolTruckMovedHandler = WL.Server.createEventHandler( { vehicle: "DeliveryTruck", refrigeration: true }, handleRefrigeratedDeliveryTruckMoved ); WL.Server.setEventHandlers( [ deviceMoveHandler, deliveryTruckMovedHandler, coolTruckMovedHandler ] );
- Mobile application logic -hybrid Android, iOS, and Windows Phone Silverlight 8
-
function wlCommonInit(){ // Common initialization code goes here. // get truck id (for example from the user) -- for this example, using a hard-coded value. var truckId = 123; var driverName = "John Smith"; var policy = { Geo: { enableHighAccuracy: true, timeout: 10000 } }; var triggers = { Geo: { tracking: { type: "PositionChange", minChangeDistance: 100, // 100 meters eventToTransmit: { event: { vehicle: "DeliveryTruck", id: truckId, driverName: driverName } } } } }; WL.Device.startAcquisition(policy, triggers); }
- Mobile application logic - native Android
-
// get truck id (for example from the user) -- for this example, using a hard-coded value. long truckId = 123; String driverName = "John Smith"; WLAcquisitionPolicy policy = new WLAcquisitionPolicy() .setGeoPolicy(new WLGeoAcquisitionPolicy() .setEnableHighAccuracy(true) .setTimeout(10000)); WLTriggersConfiguration triggers = new WLTriggersConfiguration(); triggers.getGeoTriggers().put( "tracking", new WLGeoPositionChangeTrigger() .setMinChangeDistance(100) .setEvent(new JSONObject() .put("vehicle", "DeliveryTruck") .put("id", truckId) .put("driverName", driverName))); WLClient.getInstance().getWLDevice().startAcquisition new WLLocationServicesConfiguration() .setPolicy(policy) .setTriggers(triggers));
- Mobile application logic - native iOS
-
// get truck id (for example from the user) -- for this example, using a hard-coded value. long long truckId = 123; NSString* driverName = @"John Smith"; WLAcquisitionPolicy* policy = [[WLAcquisitionPolicy alloc] init] [ setGeoPolicy: [[ [[WLGeoAcquisitionPolicy alloc] init] setEnableHighAccuracy: true] setTimeout: 10000]]; WLTriggersConfiguration* triggers = [[WLTriggersConfiguration alloc] init]; [[triggers getGeoTriggers] setObject: [[ [[WLGeoPositionChangeTrigger alloc] init] setMinChangeDistance: 100] setEvent: [NSDictionary dictionaryWithObjectsAndKeys: @"DeliveryTruck", @"vehicle", truckId, @"id", driverName, @"driverName", nil]] forKey:@"tracking"]; [[[WLClient sharedInstance] getWLDevice] startAcqusition: [[ [[WLLocationServicesConfiguration alloc] init] setPolicy: policy] setTriggers: triggers]];