Enable Live Data

Last updated:

As opposed to static data, which does not change unless data is synchronized, Live Data can change in real time, and these changes can be instantly reflected on the map and in searches.

Common use-cases are:

  • Changing the appearance of meeting rooms or workspace desks on a map, or in a list, based on occupancy information. For example, change the icon in order to indicate that a room is occupied.
  • Changing the position of a POI representing a vehicle.

Support for Live Data requires that server-side integrations are in place. For example, visualizing live occupancy data requires that a calendar or booking system integration is in place. An integration like that is set up in collaboration with MapsPeople.

The following section rely on the existence of Live Position Data. If you do not have access to a MapsIndoors Dataset that have a Live Data integration, you should use our demo API key: d876ff0e60bb430b8fabb145.

To enable Live Data in your web app, create an instance of LiveDataManager. Call the method enableLiveData() on it with a Domain Type.

This should be done after you have initialized your MapsIndoors instance, since the instance must be given as argument in the LiveDataManager constructor:

// main.js

const mapViewOptions = {
element: document.getElementById('map'),
center: { lat: 38.8974905, lng: -77.0362723 }, // The White House
zoom: 17,
maxZoom: 22,
};
const mapViewInstance = new mapsindoors.mapView.GoogleMapsView(mapViewOptions);
+ const mapsIndoorsInstance = new mapsindoors.MapsIndoors({ mapView: mapViewInstance });
const googleMapsInstance = mapViewInstance.getMap();

const externalDirectionsProvider = new mapsindoors.directions.GoogleMapsProvider();
const miDirectionsServiceInstance = new mapsindoors.services.DirectionsService(externalDirectionsProvider);
const directionsRendererOptions = { mapsIndoors: mapsIndoorsInstance }
const miDirectionsRendererInstance = new mapsindoors.directions.DirectionsRenderer(directionsRendererOptions);

+ // Enable Live Data
+ const liveDataManagerInstance = new mapsindoors.LiveDataManager(mapsIndoorsInstance);
+ liveDataManagerInstance.enableLiveData(mapsindoors.LiveDataManager.LiveDataDomainTypes.POSITION);

// Floor Selector
const floorSelectorElement = document.createElement('div');
new mapsindoors.FloorSelector(floorSelectorElement, mapsIndoorsInstance);
googleMapsInstance.controls[google.maps.ControlPosition.RIGHT_TOP].push(floorSelectorElement);

function onSearch() {
const searchInputElement = document.querySelector('input');
const searchResultsElement = document.getElementById('search-results');

const searchParameters = { q: searchInputElement.value };
mapsindoors.services.LocationsService.getLocations(searchParameters).then(locations => {
// Reset search results list
searchResultsElement.innerHTML = null;

// Append new search results
locations.forEach(location => {
const listElement = document.createElement('li');
listElement.innerHTML = location.properties.name;
// Add click event listener
listElement.addEventListener("click", () => getRoute(location), false);
searchResultsElement.appendChild(listElement);
});
// Filter map to only display search results
mapsIndoorsInstance.filter(locations.map(location => location.id), false);
});
}

function getRoute(location) {
const originLocationCoordinate = { lat: 38.897389429704695, lng: -77.03740973527613, floor: 0 }; // Oval Office, The White House (Hardcoded coordinate and floor index)
const destinationCoordinate = { lat: location.properties.anchor.coordinates[1], lng: location.properties.anchor.coordinates[0], floor: location.properties.floor };

// Route parameters
const routeParameters = {
origin: originLocationCoordinate,
destination: destinationCoordinate,
travelMode: document.getElementById('travel-modes').value.toUpperCase()
};

// Get route from directions service
miDirectionsServiceInstance.getRoute(routeParameters).then((directionsResult) => {
// Use directions render to display route
miDirectionsRendererInstance.setRoute(directionsResult);
});
}
// main.js

const miMapElement = document.querySelector('mi-map-googlemaps');
const miSearchElement = document.querySelector('mi-search');
const miListElement = document.querySelector('mi-list');

let miDirectionsServiceInstance;
let miDirectionsRendererInstance;

miMapElement.addEventListener('mapsIndoorsReady', () => {
miMapElement.getMapInstance().then((mapInstance) => {
mapInstance.setCenter({ lat: 38.8974905, lng: -77.0362723 }); // The White House
});

miMapElement.getDirectionsServiceInstance().then((directionsServiceInstance) => miDirectionsServiceInstance = directionsServiceInstance);

miMapElement.getDirectionsRendererInstance().then((directionsRendererInstance) => miDirectionsRendererInstance = directionsRendererInstance);

miMapElement.getMapsIndoorsInstance().then((mapsIndoorsInstance) => {
+ // Enable Live Data
+ const liveDataManagerInstance = new mapsindoors.LiveDataManager(mapsIndoorsInstance);
+ liveDataManagerInstance.enableLiveData(mapsindoors.LiveDataManager.LiveDataDomainTypes.POSITION);
});
})

miSearchElement.addEventListener('results', (event) => {
// Reset search results list
miListElement.innerHTML = null;

// Append new search results
event.detail.forEach(location => {
const miListItemElement = document.createElement('mi-list-item-location');
miListItemElement.location = location;
// Add click event listener
miListItemElement.addEventListener("click", () => getRoute(location), false);
miListElement.appendChild(miListItemElement);
});

// Get the MapsIndoors instance
miMapElement.getMapsIndoorsInstance().then((mapsIndoorsInstance) => {
// Filter map to only display search results
mapsIndoorsInstance.filter(event.detail.map(location => location.id), false);
});
});

function getRoute(location) {
const originLocationCoordinate = { lat: 38.897389429704695, lng: -77.03740973527613, floor: 0 }; // Oval Office, The White House (Hardcoded coordinate and floor index)
const destinationCoordinate = { lat: location.properties.anchor.coordinates[1], lng: location.properties.anchor.coordinates[0], floor: location.properties.floor };

// Route parameters
const routeParameters = {
origin: originLocationCoordinate,
destination: destinationCoordinate,
travelMode: document.getElementById('travel-modes').value.toUpperCase()
};

// Get route from directions service
miDirectionsServiceInstance.getRoute(routeParameters).then((directionsResult) => {
// Use directions render to display route
miDirectionsRendererInstance.setRoute(directionsResult);
});
}
// main.js

const mapViewOptions = {
accessToken: 'YOUR_MAPBOX_ACCESS_TOKEN',
element: document.getElementById('map'),
center: { lat: 38.8974905, lng: -77.0362723 }, // The White House
zoom: 17,
maxZoom: 22,
};
const mapViewInstance = new mapsindoors.mapView.MapboxView(mapViewOptions);
+ const mapsIndoorsInstance = new mapsindoors.MapsIndoors({ mapView: mapViewInstance });
const mapboxInstance = mapViewInstance.getMap();

const externalDirectionsProvider = new mapsindoors.directions.MapboxProvider();
const miDirectionsServiceInstance = new mapsindoors.services.DirectionsService(externalDirectionsProvider);
const directionsRendererOptions = { mapsIndoors: mapsIndoorsInstance }
const miDirectionsRendererInstance = new mapsindoors.directions.DirectionsRenderer(directionsRendererOptions);

+ // Enable Live Data
+ const liveDataManagerInstance = new mapsindoors.LiveDataManager(mapsIndoorsInstance);
+ liveDataManagerInstance.enableLiveData(mapsindoors.LiveDataManager.LiveDataDomainTypes.POSITION);

// Floor Selector
const floorSelectorElement = document.createElement('div');
new mapsindoors.FloorSelector(floorSelectorElement, mapsIndoorsInstance);
mapboxInstance.controls[google.maps.ControlPosition.RIGHT_TOP].push(floorSelectorElement);

function onSearch() {
const searchInputElement = document.querySelector('input');
const searchResultsElement = document.getElementById('search-results');

const searchParameters = { q: searchInputElement.value };
mapsindoors.services.LocationsService.getLocations(searchParameters).then(locations => {
// Reset search results list
searchResultsElement.innerHTML = null;

// Append new search results
locations.forEach(location => {
const listElement = document.createElement('li');
listElement.innerHTML = location.properties.name;
// Add click event listener
listElement.addEventListener("click", () => getRoute(location), false);
searchResultsElement.appendChild(listElement);
});
// Filter map to only display search results
mapsIndoorsInstance.filter(locations.map(location => location.id), false);
});
}

function getRoute(location) {
const originLocationCoordinate = { lat: 38.897389429704695, lng: -77.03740973527613, floor: 0 }; // Oval Office, The White House (Hardcoded coordinate and floor index)
const destinationCoordinate = { lat: location.properties.anchor.coordinates[1], lng: location.properties.anchor.coordinates[0], floor: location.properties.floor };

// Route parameters
const routeParameters = {
origin: originLocationCoordinate,
destination: destinationCoordinate,
travelMode: document.getElementById('travel-modes').value.toUpperCase()
};

// Get route from directions service
miDirectionsServiceInstance.getRoute(routeParameters).then((directionsResult) => {
// Use directions render to display route
miDirectionsRendererInstance.setRoute(directionsResult);
});
}
// main.js

const miMapElement = document.querySelector('mi-map-mapbox');
const miSearchElement = document.querySelector('mi-search');
const miListElement = document.querySelector('mi-list');

let miDirectionsServiceInstance;
let miDirectionsRendererInstance;

miMapElement.addEventListener('mapsIndoorsReady', () => {
miMapElement.getMapInstance().then((mapInstance) => {
mapInstance.setCenter({ lat: 38.8974905, lng: -77.0362723 }); // The White House
});

miMapElement.getDirectionsServiceInstance().then((directionsServiceInstance) => miDirectionsServiceInstance = directionsServiceInstance);

miMapElement.getDirectionsRendererInstance().then((directionsRendererInstance) => miDirectionsRendererInstance = directionsRendererInstance);

miMapElement.getMapsIndoorsInstance().then((mapsIndoorsInstance) => {
+ // Enable Live Data
+ const liveDataManagerInstance = new mapsindoors.LiveDataManager(mapsIndoorsInstance);
+ liveDataManagerInstance.enableLiveData(mapsindoors.LiveDataManager.LiveDataDomainTypes.POSITION);
+ });
})

miSearchElement.addEventListener('results', (event) => {
// Reset search results list
miListElement.innerHTML = null;

// Append new search results
event.detail.forEach(location => {
const miListItemElement = document.createElement('mi-list-item-location');
miListItemElement.location = location;
// Add click event listener
miListItemElement.addEventListener("click", () => getRoute(location), false);
miListElement.appendChild(miListItemElement);
});

// Get the MapsIndoors instance
miMapElement.getMapsIndoorsInstance().then((mapsIndoorsInstance) => {
// Filter map to only display search results
mapsIndoorsInstance.filter(event.detail.map(location => location.id), false);
});
});

function getRoute(location) {
const originLocationCoordinate = { lat: 38.897389429704695, lng: -77.03740973527613, floor: 0 }; // Oval Office, The White House (Hardcoded coordinate and floor index)
const destinationCoordinate = { lat: location.properties.anchor.coordinates[1], lng: location.properties.anchor.coordinates[0], floor: location.properties.floor };

// Route parameters
const routeParameters = {
origin: originLocationCoordinate,
destination: destinationCoordinate,
travelMode: document.getElementById('travel-modes').value.toUpperCase()
};

// Get route from directions service
miDirectionsServiceInstance.getRoute(routeParameters).then((directionsResult) => {
// Use directions render to display route
miDirectionsRendererInstance.setRoute(directionsResult);
});
}

In the example above we create an instance of LiveDataManager and enable Live Data for the "Position" Domain type.

Using the demo API key you should now be able to see a "Staff Person" moving from one end to the other at ground floor in The White House main building.

Learn more about controlling and rendering Live Data in MapsIndoors in the introduction to Live Data.

If you have followed the tutorial exactly so far, you will now have something like this (due to technical limitations, only the Google Maps result is shown on this page, but Mapbox implementation will also work if you followed this guide):

Summary

Congratulations! You're at the end of your journey (for now), and you've accomplished a lot! 🎉

  • You learned which prerequisites is needed to start building with MapsIndoors.
  • You loaded a interactive map with MapsIndoors locations and added a floor selector for navigating between floors.
  • You created a search experience to search for specific locations on the map.
  • You added functionality for getting directions from one Location to another.
  • You learned how to enable different types of Live Data Domains in your app.

This concludes the "Getting Started" tutorial, but there's always more to discover. To get more inspiration on what to build next please visit our showcase page to see how other clients use MapsIndoors! For more documentation, please visit the rest of our Docs site!.