Location Data Sources

In this tutorial we will show how you can build a custom Location Source, representing locations of robot vacuums. The robots locations will be served from a mocked list and displayed on a map.

We will start by creating our implementation of a location source.

Create the class RobotVacuumLocationSource that implements MPLocationSource:

Implement the methods from MPLocationSource and extend the constructor of the RobotVacuumLocationSource to accept a list of locations that will represent the Robot vacuums.

class RobotVacuumLocationSource(private val robots: ArrayList<MPLocation>): MPLocationSource {
    private val mObservers = ArrayList<MPLocationsObserver>()
    private var mStatus = MPLocationSourceStatus.NOT_INITIALIZED
    override fun getLocations(): MutableList<MPLocation> {
        return robots
    }
    override fun addLocationsObserver(observer: MPLocationsObserver?) {
        if (observer != null) {
            mObservers.add(observer)
        }
    }
    override fun removeLocationsObserver(observer: MPLocationsObserver?) {
        if (observer != null) {
            mObservers.remove(observer)
        }
    }
    private fun notifyUpdateLocations() {
        for (observer in mObservers) {
            observer.onLocationsUpdated(robots, this)
        }
    }
    override fun getStatus(): MPLocationSourceStatus {
        return mStatus
    }
    override fun getSourceId(): Int {
        return 10101010
    }
    override fun clearCache() {
        robots.clear()
        mObservers.clear()
    }
    override fun terminate() {
        robots.clear()
        mObservers.clear()
    }
}

Create a Fragment or Activity that contains a map with MapsIndoors loaded.

Add a BASE_POSITION MPLatLng that will be used to calculate a random location for the Robot Vacuums.

Then we need to add some variables:

Create the baseDisplayRule after MapsIndoors has loaded:

create a method to setup the RobotVacuumLocationSource inside your fragment:

As seen in the example above we add the RobotVacuumLocationSource through MapsIndoors.addLocationSources and call RobotVacuumLocationSource.setup()

This method sets the status to of the source to available and notifies MapsIndoors that locations are updated.

In the setupLocationSource method we call generateLocations to populate the location list with new locations:

Create the startUpdatingPositions method that calls updateLocations every second:

Create a method that can stop the positions updates at any time:

Create a method called updateLocations that will update the position of the Locations:

See the samples in the locationsources folder

Last updated

Was this helpful?