Overview
ThePropertyMap component provides Google Maps integration for displaying property locations. The package includes two variants: a standard read-only map and a draggable version for location editing.
Import
PropertyMap (Read-Only)
Standard map component for displaying a fixed property location.Props
Latitude coordinate of the property location
Longitude coordinate of the property location
Title displayed on the map marker tooltip
Additional CSS classes to apply to the map container
Height of the map container (CSS value)
Initial zoom level for the map (1-20)
Usage Examples
DraggablePropertyMap (Interactive)
Map component with a draggable marker for location editing in forms.Props
Inherits all props fromPropertyMap plus:
Callback function triggered when the marker is dragged to a new position. Receives the new latitude and longitude.
Whether the marker can be dragged. Set to false for a read-only draggable map.
Usage Examples
Features
Automatic Map Initialization
Both components handle Google Maps API loading automatically:- Lazy loads the Google Maps JavaScript API
- Uses
@googlemaps/js-api-loaderpackage - Caches the API instance for better performance
- Includes Places library for additional features
Error Handling
Graceful error handling for missing data:- Displays fallback UI when coordinates are missing
- Shows error message if API key is not configured
- Console logging for debugging
Fallback UI
When coordinates are invalid or missing:Marker Features
Read-Only Map
- Fixed marker position
- Marker title on hover
- Automatic centering
Draggable Map
- Draggable marker (when
draggable={true}) dragendevent triggersonLocationChange- Real-time coordinate updates
Map Controls
Both components configure the map with:- Disabled map type control
- Disabled street view control
- Enabled fullscreen control
- Greedy gesture handling (no Ctrl+scroll requirement)
Configuration
Environment Variables
Both components require the Google Maps API key:Google Maps API Setup
- Get an API key from Google Cloud Console
- Enable the following APIs:
- Maps JavaScript API
- Places API (optional, for future features)
- Set up billing (required by Google)
- Add the key to your
.envfile
Google Maps Loader
The components use a shared loader utility atsrc/lib/googleMaps.ts:1:
- Caches the Google Maps instance
- Prevents multiple API loads
- Returns a promise that resolves to the
googleobject
Responsive Design
Both components are fully responsive:- Container uses provided height (default 400px)
- Width automatically fills parent container
- Touch-friendly on mobile devices
- Gesture handling optimized for mobile
Accessibility
- Map container has
tabIndex={0}for keyboard focus - Descriptive
aria-labelattributes - DraggablePropertyMap includes drag instructions in label
Aria Labels
PropertyMap:Lifecycle Management
Both components properly clean up resources:- Removes markers on unmount
- Clears map instance references
- Cancels pending API loads
TypeScript Types
The components define internal types for Google Maps objects:Dependencies
@googlemaps/js-api-loader- Google Maps API loaderreact- Hooks (useEffect, useRef)
Source Code
- Standard map:
src/components/PropertyMap.tsx:42 - Draggable map:
src/components/PropertyMap.tsx:141 - Maps loader utility:
src/lib/googleMaps.ts:1