Skip to content

Way Finding

The WayFinder class allows you to build a routing graph by loading individual layers from GeoJSON files. Layers can represent a floor of a building or an outdoor level.

To set up the graph, you first need to add all the required layers by calling WayFinder.loadLayer, providing both a layer name and the path to the corresponding GeoJSON file.

Once all layers are loaded, you must finalize the graph by calling WayFinder.linkLayers. This step links the layers together and completes the graph structure. After calling WayFinder.linkLayers, no further layers can be added.

At this point, the routing graph is ready, and you can use it to compute routes between points across the defined layers.

Add source and destination(s)

Once all layers have been loaded, you must first call WayFinder.addSource before adding any destinations. If WayFinder.linkLayers hasn’t been explicitly called beforehand, it will be automatically invoked internally when WayFinder.addSource is executed. Only one source can be set per routing session.

After the source is defined, you can call WayFinder.addDestination multiple times to add the desired destination points for route computation.

If you need to start over (i.e. change the source and destinations), simply call WayFinder.reset, which allows you to define a new source and reinitialize the process.

Computing routes

You can compute either a single route or multiple routes (to different destinations) in a single pass.

To compute a single route, use WayFinder.findShortestPath.

  • If you’ve specified one source and one destination, this method will return the shortest path between them, if one exists - otherwise, it returns null.
  • If multiple destinations have been added, it returns the shortest path from the source to any of the specified destinations, again returning null if no path is found.

To compute multiple routes at once, use WayFinder.findShortestPaths (note the plural). This returns a RouteSet containing all successfully computed routes.

Optionally, you can pass true to the addEmpty parameter, in this way the method will return one entry per destination: if a path to a specific destination cannot be found, the corresponding entry in the RouteSet will be null.

Example

The example below demonstrates how to calculate the shortest route between two points. Note the use of the use extension function to automatically release native resources.

WayFinder().use { finder ->
    finder.loadLayer("Piano1", "piano1.geojson")
    finder.loadLayer("Piano2", "piano2.geojson")
    finder.addSource("Piano1", 1.0, 2.5)
    finder.addDestination("Piano2", 10.0, 5.0)
    finder.findShortestPath()?.use { route ->
        for (node in route.nodes) {
            println("Node: ${node.x} ${node.y}")
        }
    } ?: println("No route found")
}

The following example demonstrates how to compute the shortest routes from a single source to two destinations in one pass.

WayFinder().use { finder ->
    finder.loadLayer("Piano1", "piano1.geojson")
    finder.loadLayer("Piano2", "piano2.geojson")
    finder.addSource("Piano1", 1.0, 2.5)
    finder.addDestination("Piano2", 10.0, 5.0)
    finder.addDestination("Piano2", 8.0, 20.0)
    finder.findShortestPaths(true).use { routeSet ->
        var i = 0
        for (route in routeSet.routes) {
            ++i
            route?.let {
                for (node in it.nodes) {
                    println("Node: ${node.x} ${node.y}")
                }
                it.release()
            } ?: println("No route found for destination $i")
        }
    }