Live Data in Practice
In this tutorial you will learn to work with Live Updates / real-time data in MapsIndoors. It is recommended that you read the Live Data Introduction before continuing.
We will create a simple map showing dynamic changes initiated from Live Data sources known by MapsIndoors. If you do not have a Live Data integration in place for your MapsIndoors project, you can use API key d876ff0e60bb430b8fabb145
for demo and test purposes. The test data are of the Occupancy Domain Type and the Position Domain Type.
Start by creating an activity that has a map view and a MapControl
initiated.
Add buttons to the view for toggling subscriptions create one for enabling positioning and one for enabling occupancy.
Set a click listener. This example uses the convenience interface to setup Live Data for the app.
If you are using the Demo Solution, you will need to align the camera with one of the Solution's Venues. You can use this example to find a Venue and position the camera on it.
And that's it! You now have Live Data running on your app. If you need to show Live Data in another way, you can add handlers for it. Read more about this in Rendering Live Data Locations.
Using Live Data Without the "Convenience Interface"
If you need a different way of handling Live Data subscriptions completely, because of the limitations of the "convenience interface", it is also possible to implement your own way of setting up subscriptions and dealing with the responses.
Say you only need it for a few specific Locations with positioning updates. You use those Location's IDs to create a LiveTopicCriteria
with the builder supplied on the class that matches what you want. Once you have created the LiveTopicCriteria
, you can subscribe to it through the LiveDataManager
.
Once subscribed, the Location will update its position according to the Live Data it receives. If you want to disable the subscription later on, you can store the LiveTopicCriteria
, and unsubscribe through LiveDataManager.unsubscribeTopic(LiveTopicCritera liveTopicCriteria)
. The lifecycle of the subscription is already handled so there is no need to unsubscribe and resubscribe on Android lifecycles. If you want to see the position update happening, you can set up a listener through the LiveDataManager
like in this example.
This is a really simple implementation to get Live Data up and running.
Now let's say you want to get the Occupancy of your Locations and make the label update to reflect how many people are inside a room. First we will implement a way to setup LiveTopics
so we don't get too much data at once. Here we will create a LiveTopicCriteria
that uses the currently viewed Building together with listening on Occupancy updates. We will assign the LiveTopicCriteria
to a variable so that we can unsubscribe when changing to another topic.
Now if you already have the live update listener from the previous Position example, you can remove the Positioning part on a specific Location, and instead implement a more generic way of handling the new updates we subscribe to. Here is an example that updates the label with the amount of people in each room.
You should now be able to move between Buildings and see labels being updated with the Live Data you have requested.
Last updated