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
Completion of Step 2: Create a Search Experience.
Your MapsIndoors API Key and Google Maps API Key should be correctly set up. We will continue using the demo API key
02c329e6777d431a88480a09and venue IDdfea941bb3694e728df92d3dfor 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
divwithid="search-ui"and classflex-column. This helps in managing the visibility of the entire search section.A new
divwithid="details-ui"is added within the.panel. This container will hold the location's name, description, and a close button.It has the class
hiddento be invisible by default.It also has
flex-columnfor 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. Theflex-columnclass 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-wrapto respect newlines from the data,max-heightandoverflow-yfor 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
handleLocationClickfunction manages map interactions (selecting a location, zooming, and changing floors).This function then calls the
showDetailsfunction 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()andhideDetailsUI(): 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.nameandlocation.properties.descriptionto 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) andshowDetails(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.htmlmatch those used inscript.jsforgetElementById.Ensure
location.properties.nameandlocation.properties.descriptionexist for the selected location or that default text is handled.Confirm CSS for
.hidden,#search-ui, and#details-uiis correctly applied and toggled.
Map doesn't navigate or select location:
Ensure
mapsIndoorsInstanceis correctly initialized.Verify the
locationobject passed tohandleLocationClickis a valid object conforming to theLocationinterface.Check for console errors when
selectLocation,goTo, orsetFloorare called.
"Close" button doesn't work:
Ensure the event listener is correctly attached to
detailsCloseButtonand thatshowSearchUIis called.
Next Steps
You've now enhanced your application to display detailed information about locations. Users can not only find locations but also learn more about them.
Next, you'll learn how to get and display directions between locations:
Step 4: Getting Directions
Last updated
Was this helpful?