LogoLogo
CMSGitHubSupportBook a demo
  • Documentation
  • Academy
  • Help Center
  • Welcome
  • SDKS & Frameworks
    • Web
      • Getting Started
        • Prerequisites
          • MapsIndoors
          • Map Engine Provider
            • Option 1: Get your Mapbox Access Token
            • Option 2: Get your Google Maps API Keys​
          • Map Engine Setup
        • Getting Started: MapsIndoors
      • Map Visualization
        • Highlight, Hover and Select
        • Remove Labels from Buildings and Venues
        • Change Building Outline
        • Managing Collisions Based on Zoom Level
        • 3D Maps
          • Managing your 3D Maps
        • Base Map Styling - Google Maps
        • Managing feature visibility for Mapbox
      • Wayfinding
        • Directions
        • Directions Service
          • Tailoring the directions to your specific needs
        • Directions Renderer
          • Customizing the Route Animation
        • Multi-stop navigation
          • Custom Icons
        • User's Location as Point of Origin
      • Search
        • Search Operations
        • Searching
        • Using External ID, Geospatial Joins
        • Utilizing MapsIndoors Web Components and Other Searches
      • Map Management
      • Data Visualization
        • Display Heatmap Overlay
      • Other guides
        • Authentication
          • Single Sign-On
            • SSO Configuration
            • SSO Authorisation
          • 2-Factor Authentication
          • Password Reset
        • Application User Roles
        • Custom Properties
        • Display Language
        • Language
        • User Positioning
          • Show User's Location aka. Blue Dot
          • Using Cisco DNA Spaces
        • Working with Events
        • Turn Off Collisions Based on Zoom Level
        • Remove Labels from Buildings and Venues for Web
        • Synchronizing data for a subset of venues
        • Custom Floor Selector
      • Display Rules in Practice
      • Offline Data
      • Managing map visibility
    • Android
      • Getting Started
        • Prerequisites
        • Create a New Project
        • Show a Map
        • Create a Search Experience
        • Getting Directions
        • Enable Live Data
        • Integrating MapsIndoors into your own App
        • Migrating from V3 to V4
          • Migrating to Mapbox V11
      • Directions
        • Directions Service
        • Directions Renderer
          • User's Location as Point of Origin
        • Wayfinding Instructions
          • See Route Element Details
        • Using multi-stop navigation
      • Searching
        • Searching on a Map
        • Creating a Search Experience
      • Switching Solutions
      • Caching & Offline Data
      • Display Language
      • Displaying Objects
        • Application User Roles
        • Getting a Polygon from a Location
        • Location Clustering
        • Location Data Sources
        • Location Details
        • Turn Off Collisions Based on Zoom Level
        • Enabling and Disabling features on the map
      • Change Building Outline Color
      • Event Logging
      • Configuring a menu with AppConfig
      • Display Heatmap Overlay
      • Custom Properties
      • Custom Floor Selector
      • External IDs
      • User Positioning
        • Show User's Location aka. Blue Dot
        • Using Cisco DNA Spaces
        • Using Google Fused Location Provider
        • Using Indoor Atlas
      • Authentication
        • Single Sign-On
          • SSO Configuration
          • SSO Authorisation
        • 2-Factor Authentication
        • Password Reset
      • Display Rules in Practice
        • Label styling through Display Rules
      • Highlight and Select
    • iOS
      • Getting Started
        • Prerequisites
        • Set Up Your Environment
        • Display a Map
        • Search
        • Getting Directions
        • Migrating from v3 to v4
      • Directions
        • Directions Renderer
          • User's Location as Point of Origin
        • Wayfinding Instructions
          • See Route Element Details
        • Directions Service
        • Using multi-stop navigation
      • Searching
        • Searching on a Map
        • Creating a Search Experience
      • Caching & Offline Data
      • Displaying Objects
        • Application User Roles
        • Getting a Polygon from a Location
        • Location Details
        • Turn Off Collisions Based on Zoom Level
        • Enabling and Disabling features on the map
      • Custom Floor Selector
      • Change Building Outline Color
      • Custom Map Padding
      • Custom Properties
      • Display Rules in Practice
        • Label styling through Display Rules
      • Switching Solutions
      • Show User's Location aka. Blue Dot
        • Using Indoor Atlas
        • Using Cisco DNA Spaces
      • Highlight and Select
      • Display Language
    • React Native
      • Getting Started
        • Prerequisites
        • Project Setup
        • Displaying a Map
        • Creating a Search Experience
        • Getting Directions
        • Enabling Live Data
      • Showing Blue Dot
    • Flutter
      • Getting Started
        • Prerequisites
        • Create a New Project
        • Show a Map
        • Create a Search Experience
        • Getting Directions
      • Migration Guide
    • Integration API
      • Integration API Access
        • Access with Swagger
        • Access with Postman
        • Access with Python
        • Client credentials flow
      • Data Description
      • Reverse Geocoding
      • Route Access
      • OpenAPI Specification
    • Built-In Map Edits
      • Getting started
      • Authentication
      • Release notes
      • Reference docs
  • Products
    • Product Overview
    • CMS
      • Interface Overview
      • Display Rules
      • Media Library
        • 2D Models and Icons
        • 3D Models
      • Editing Data
      • Solution Settings
      • Settings
      • Data Concepts
      • User Roles
      • Route Network
        • Barrier Route Element
        • Door Route Element
      • Additional Location Details
    • Map Template
      • Getting Started
        • Web Component
        • React Component
      • Configuration
        • Query Parameters
      • Customization
      • Deploying Map Template to a cloud storage provider
      • 2D/3D Visibility Switch
      • External customization of the Map Template
      • Location Details configuration
      • Kiosk
        • QR code configuration
  • Other
    • Design
      • Standard MapsIndoors Map Style
      • Using a Custom Mapbox MapStyle
    • Changelog
      • Web SDK
        • V4
        • V3
      • Android SDK
        • V4
        • V3
      • iOS SDK
        • V4
        • V3
      • React Native SDK
      • Flutter SDK
      • MI Components
      • Map Template
    • Glossary
  • Legacy Docs
    • Android SDK V3
      • Getting Started
        • Prerequisites
        • Create a New Project
        • Show a Map
        • Create a Search Experience
        • Getting Directions
        • Enable Live Data
        • Integrating MapsIndoors into your own App
    • iOS SDK V3
      • Getting Started
        • Prerequisites
        • Set Up Your Environment
        • Display a Map
        • Search
        • Directions
        • Live Data
        • Integrating MapsIndoors into your own App
      • Inspect Route Element for iOS v3
      • Using Cisco DNA Spaces
      • Using Indoor Atlas
      • Switching Solutions
      • Show User's Location aka. Blue Dot
      • Application User Roles
      • Getting a Polygon from a Location
      • Location Details
  • MapsIndoors SDK Firewall
  • Google Analytics & Logging
  • Reference Docs
    • Web SDK
    • Android SDK
    • iOS SDK
    • React Native SDK
    • Flutter SDK
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. SDKS & Frameworks
  2. Android
  3. Directions

Wayfinding Instructions

Android v4

Last updated 11 months ago

Was this helpful?

This tutorial will show how to work with the route model returned from a directions service call. It will also show how you can utilize interactions between the route rendering on the map, and text-based instructions showed in another view.

This tutorial will be based off the MapsIndoors Samples example found here: .

An example of the view XML file for the WayfindingFragment this guide will use can be found here: .

First, create variables for MPLocation and MPRoute objects to use later in describing wayfinding. Also create a Variable for MPDirectionsRenderer so we can control rendering through changes in the ViewPager.

private var mRoute: MPRoute? = null
private var mLocation: MPLocation? = null

Next step we will create the Fragment that will contain a short description of each Leg of the route inside the ViewPager

An example of the view XML file for the RouteLegFragment, that this guide will use, can be found here: .

We will start by adding the code inside the WayfindingFragment.

First, start by changing the code inside onCreateView.

override fun onCreateView(view: View, @Nullable savedInstanceState: Bundle?) {
    _binding = FragmentWayfindingBinding.inflate(inflater, container, false)

    MapsIndoors.load(requireActivity().applicationContext, "gettingstarted", null)

    val routeLegAdapter = RouteCollectionAdapter(this)
    val viewPager = binding.stepViewPager
    viewPager.adapter = routeLegAdapter
    viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
        }
    })

    val root: View = binding.root
    val supportMapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
    mMapView = supportMapFragment.view
    supportMapFragment.getMapAsync(this)
    return root
}

Next we will create FragmentStateAdapter that will be used on the ViewPager to contain RouteLegFragments

inner class RouteCollectionAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
    override fun getItemCount(): Int {
        mRoute?.legs?.let { legs->
            return legs.size
        }
        return 0
    }

    override fun createFragment(position: Int): Fragment {
        if (position == mRoute?.legs?.size!! - 1) {
            return RouteLegFragment.newInstance("Walk to " + mLocation?.name, mRoute?.legs!![position]?.distance?.toInt(), mRoute?.legs!![position]?.duration?.toInt())
        } else {
            var leg = mRoute?.legs!![position]
            var firstStep = leg.steps.first()
            var lastFirstStep = mRoute?.legs!![position + 1].steps.first()
            var lastStep = mRoute?.legs!![position + 1].steps.last()

            var firstBuilding = MapsIndoors.getBuildings()?.getBuilding(firstStep.startPoint.latLng)
            var lastBuilding  = MapsIndoors.getBuildings()?.getBuilding(lastStep.startPoint.latLng)
            return if (firstBuilding != null && lastBuilding != null) {
                RouteLegFragment.newInstance(getStepName(lastFirstStep, lastStep), leg.distance.toInt(), leg.duration.toInt())
            }else if (firstBuilding != null) {
                RouteLegFragment.newInstance("Exit: " + firstBuilding.name,  leg.distance.toInt(), leg.duration.toInt())
            }else {
                RouteLegFragment.newInstance("Enter: " + lastBuilding?.name,  leg.distance.toInt(), leg.duration.toInt())
            }
        }
    }
}

Let's create a method to textually describe each Leg. This method creates a string that takes the first and last step of the next leg to create a description for the user on what to do at the end of the currently shown leg. You will also create a method to get a list of the different highway types the route can give the user. These are found as enums through the MPHighway class in the MapsIndoors SDK.

fun getStepName(startStep: MPRouteStep, endStep: MPRouteStep): String {
    val startStepZindex: Double = startStep.startLocation!!.zIndex
    val startStepFloorName: String = startStep.startLocation.floorName!!
    var highway: String? = null
    for (actionName in getActionNames()) {
        if (startStep.highway == actionName) {
            highway = if (actionName == MPHighway.STEPS) {
                "stairs"
            } else {
                actionName
            }
        }
    }
    if (highway != null) {
        return java.lang.String.format(
            "Take %s to %s %s",
            highway,
            "level",
            if (endStep.endLocation.floorName!!.isEmpty()) endStep.endLocation.zIndex else endStep.endLocation.floorName
        )
    }
    if (startStepFloorName == endStep.endLocation.floorName) {
        return "Walk to next step"
    }
    val endStepFloorName: String = endStep.endLocation.floorName!!
    return if (endStepFloorName.isEmpty()) java.lang.String.format(
        "Level %s to %s",
        startStepFloorName.ifEmpty { startStepZindex },
        endStep.endPoint.floorIndex
    ) else String.format(
        "Level %s to %s",
        startStepFloorName.ifEmpty { startStepZindex },
        endStepFloorName
    )
}

private fun getActionNames(): ArrayList<String> {
    val actionNames: ArrayList<String> = ArrayList()
    actionNames.add(MPHighway.ELEVATOR)
    actionNames.add(MPHighway.ESCALATOR)
    actionNames.add(MPHighway.STEPS)
    actionNames.add(MPHighway.TRAVELATOR)
    actionNames.add(MPHighway.RAMP)
    actionNames.add(MPHighway.WHEELCHAIRLIFT)
    actionNames.add(MPHighway.WHEELCHAIRRAMP)
    actionNames.add(MPHighway.LADDER)
    return actionNames
}

Now, lets create the RouteLegFragment to give context for the Legs in the WayfindingFragment

private var mStep: String? = null
private var mDuration: Int? = null
private var mDistance: Int? = null

companion object {
    @JvmStatic
    fun newInstance(step: String, distance: Int?, duration: Int?) =
        RouteLegFragment().apply {
            mStep = step
            mDistance = distance
            mDuration = duration
        }
}

You must also update the onViewCreated method to use the new views added earlier in the tutorial.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding.stepTextView.text = mStep

    if (Locale.getDefault().country == "US") {
        binding.distanceTextView.text = (mDistance?.times(3.281))?.toInt().toString() + " feet"
    }else {
        binding.distanceTextView.text = mDistance?.toString() + " m"
    }
    mDuration?.let {
        if (it < 60) {
            binding.durationTextView.text = "$it sec"
        }else {
            binding.durationTextView.text = TimeUnit.MINUTES.convert(it.toLong(), TimeUnit.SECONDS).toString() + " min"
        }
    }
}

Now you have the revised UI for providing the user with a more explanatory route description when navigating. Now it needs to be rendered onto the map.

We will create a method named getRoute that queries a route between two location, we know exist on the current solution. When we receive the route we will assign the route to the fragment as well as calling setRoute on the MPDirectionsRenderer with the received Route. We will also notify the ViewPager that the route is updated with notifyDataSetChanged.

fun getRoute() {
    val directionsService = MPDirectionsService(requireContext())
    if (mDirectionsRenderer == null) {
        mDirectionsRenderer = MPDirectionsRenderer(mMapControl!!)
    }

    directionsService.setRouteResultListener { mpRoute, miError ->
        if (miError == null && mpRoute != null) {
            mRoute = mpRoute
            mDirectionsRenderer?.setRoute(mpRoute)
            requireActivity().runOnUiThread {
                binding.stepViewPager.adapter?.notifyDataSetChanged()
            }
        }
    }
    val location = MapsIndoors.getLocationById("5a07435a4e074edc9396b2ff")
    mLocation = MapsIndoors.getLocationById("24ede0c9a5004a148bd01d96")
    if (location != null && mLocation != null) {
        directionsService.query(location.point, mLocation!!.point)
    }
}

To change the routing when swapping between tabs on the viewpager, use the call back that we added further up inside the onViewCreated of NavigationFragment.

val routeLegAdapter = RouteCollectionAdapter(this)
val viewPager = binding.stepViewPager
viewPager.adapter = routeLegAdapter
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
    override fun onPageSelected(position: Int) {
        super.onPageSelected(position)
        mDirectionsRenderer?.selectLegIndex(position)
        mDirectionsRenderer?.selectedLegFloorIndex
    }
})

You should now have a Fragment with a map that has a route rendered on it. With descriptions of each Leg of the Route inside a ViewPager that will display the Leg related to the page that is viewed.

The full working example can be found here: .

WayFinding
Wayfinding view
WayfindingFragment.kt
RouteLeg view
WayfindingFragment.kt
WayfindingFragment.kt
WayfindingFragment.kt
RouteLegFragment.kt
RouteLegFragment.kt
WayfindingFragment.kt
WayfindingFragment.kt
WayFinding