Map Context
The React HERE Maps library uses React Context to share the map instance and HERE Maps objects across components.Overview
TheHereMap component creates a context that provides access to:
- map - The HERE Maps instance (
H.Map) - platform - HERE Platform service (
H.service.Platform) - ui - HERE Maps UI object (
H.ui.UI) - behavior - Map behavior object (
H.mapevents.Behavior)
Using the Context
Built-in Components
All library components automatically consume the context:Custom Components
Access the context in your custom components usinguseMapContext:
Context Values
map (RefObject)
A React ref containing the HERE Maps instance:platform (RefObject)
Reference to the HERE Platform service for geocoding, routing, etc.:ui (RefObject)
Reference to the HERE Maps UI for controls and popups:behavior (RefObject)
Reference to map behavior for interaction handling:Advanced Examples
Map Event Listeners
Geocoding Service
Info Bubbles
Custom Controls
Best Practices
Always check for null
Always check for null
Since context values are refs, always check if they’re loaded before using:
Clean up event listeners
Clean up event listeners
Always remove event listeners in useEffect cleanup:
TypeScript types
TypeScript types
Use HERE Maps TypeScript definitions for type safety: