Show the Details

Goal: This guide demonstrates how to display detailed information about a location when a user selects it from the search results or clicks a POI on the map. The details will include the location's name and description, and the map will navigate to and highlight the selected location.

SDK Classes and Methods Introduced:

  • Retrieving and displaying specific location properties (e.g., location.properties.name, location.properties.description) within a dedicated details panel.

  • Applying mapsIndoorsInstance.deselectLocation() to manage map state when closing the details view.

Implementation Patterns:

  • Expanding the unified click handler to show location details

  • Implementing UI state management between search and details views

  • Creating a responsive details panel UI

  • Handling transitions between different UI states

Prerequisites

  • Your MapsIndoors API Key and Mapbox Access Token should be correctly set up. We will continue using the demo API key 02c329e6777d431a88480a09 and venue ID dfea941bb3694e728df92d3d for this example.

The Code

This step involves modifications to index.html to add elements for displaying details, style.css to style these new elements, and script.js to handle the logic of fetching and displaying location details and interacting with the map.

Unified Click Handler Pattern

In Step 1, we introduced a reusable click handler (handleLocationClick) for POIs on the map. In Step 2, we reused this handler for search result clicks. In this step, we expand the handler to show a details panel with more information about the selected location, and manage the UI state.

Update index.html

Open your index.html file. We will add a new div within the existing .panel to hold the location details. This div will be hidden by default and shown when a location is selected.

Explanation of index.html updates:

  • The existing search input and results list are wrapped in a div with id="search-ui" and class flex-column. This helps in managing the visibility of the entire search section.

  • A new div with id="details-ui" is added within the .panel. This container will hold the location's name, description, and a close button.

    • It has the class hidden to be invisible by default.

    • It also has flex-column for layout consistency.

  • Inside #details-ui:

    • <h3 id="details-name"></h3>: For displaying the location name.

    • <p id="details-description"></p>: For displaying the location description.

    • <button id="details-close" class="details-button">Close</button>: A button to close the details view and return to the search results.

Update style.css

Modify your style.css file to add styles for the new location details UI elements.

Explanation of style.css updates:

  • #search-ui, #details-ui: Basic styling to ensure they take full width. The flex-column class will handle their internal layout.

  • #details-name: Styles for the location name heading (font size, margin, border).

  • #details-description: Styles for the description text (font size, color, white-space: pre-wrap to respect newlines from the data, max-height and overflow-y for scrollability).

  • .details-button: General styling for buttons in the details view (padding, border-radius, full width).

  • #details-close: Specific styling for the "Close" button (background color, text color, hover effect).

Update script.js

The script.js file will be updated to handle showing/hiding the details panel, populating it with location data, and interacting with the map.

Explanation of script.js updates:

  • Unified Click Handler Implementation:

    • Building upon our approach in previous steps, we've implemented a comprehensive click handler system.

    • The handleLocationClick function manages map interactions (selecting a location, zooming, and changing floors).

    • This function then calls the showDetails function to update the UI presentation.

    • This separation of concerns makes the code more maintainable and easier to understand.

  • UI State Management Functions:

    • showSearchUI(): Shows the search interface and hides the details panel.

    • showDetailsUI(): Shows the details interface and hides the search panel.

    • hideSearchUI() and hideDetailsUI(): Helper functions to manage UI visibility.

    • showDetails(location): Updates the details UI with the location's name and description.

    • The close button in the details panel has an event listener that returns to the search UI.

  • Map Interaction Methods:

    • mapsIndoorsInstance.goTo(location): Pans and zooms the map to the selected location.

    • mapsIndoorsInstance.setFloor(location.properties.floor): Switches to the floor where the location exists.

    • mapsIndoorsInstance.selectLocation(location): Visually highlights the selected location on the map.

  • Location Properties Access:

    • We access location.properties.name and location.properties.description to display detailed information.

    • The fallback text "No description available" is shown when description is missing.

  • Search Result Click Handler:

    • Search result clicks now trigger the same handler as map POI clicks, providing a consistent experience.

    • This unified approach ensures the same behavior whether a user interacts with the map or search results.

    • The separation between handleLocationClick (for map operations) and showDetails (for UI updates) creates a cleaner architecture that's easier to maintain and extend.

  • Initial Setup:

    • showSearchUI() is called at the end to ensure the application starts with the search interface visible.

Expected Outcome

After implementing these changes:

  • When you search for locations and click on a result in the list:

    • The search input and results list will be hidden.

    • A details panel will appear showing the selected location's name and description.

    • The map will pan and zoom to the selected location.

    • The selected location will be highlighted on the map.

    • The map will switch to the correct floor of the selected location.

  • Clicking the "Close" button in the details panel will hide the details and show the search input and results list again.

  • When you click a POI on the map:

    • The details panel will appear showing the location's name and description.

    • The map will pan and zoom to the selected location.

    • The selected location will be highlighted on the map.

    • The map will switch to the correct floor of the selected location.

Troubleshooting

  • Details panel doesn't show or shows incorrect data:

    • Check browser console for errors.

    • Verify IDs in index.html match those used in script.js for getElementById.

    • Ensure location.properties.name and location.properties.description exist for the selected location or that default text is handled.

    • Confirm CSS for .hidden, #search-ui, and #details-ui is correctly applied and toggled.

  • Map doesn't navigate or select location:

    • Ensure mapsIndoorsInstance is correctly initialized.

    • Verify the location object passed to handleLocationClick is a valid object conforming to the Location interface.

    • Check for console errors when selectLocation, goTo, or setFloor are called.

  • "Close" button doesn't work:

    • Ensure the event listener is correctly attached to detailsCloseButton and that showSearchUI is called.

Next Steps

You've now enhanced your application to display detailed information about locations using a unified click handler. Next, you'll learn how to get and display directions between locations:

Last updated

Was this helpful?