Overview
TheBaseMapComponent is a standalone Angular component that provides a ready-to-use Google Maps integration with automatic script loading, responsive sizing, and configurable map options. It handles the complexity of loading the Google Maps JavaScript API and provides a clean interface for embedding maps in your application.
Component Metadata
The component selector used in templates
This is a standalone component that can be imported directly without a module
Required imports:
GoogleMapsModule, IonSpinnerInput Properties
The height of the map component. Can be specified as:
- A string with CSS units (e.g.,
'100%','500px','50vh') - A number (automatically converted to pixels)
The width of the map component. Can be specified as:
- A string with CSS units (e.g.,
'100%','800px','80vw') - A number (automatically converted to pixels)
Map Configuration
Default Center and Zoom
The component initializes with default map coordinates:Map Options
The component configures Google Maps with the following default options:Google Maps API key from environment configuration (
environment.googleMapsApiKey)Disables the default Google Maps UI controls
Enables immediate gesture handling without requiring Ctrl/Cmd for zooming
Allows users to click on map icons for more information
Features
Automatic Script Loading
The component automatically loads the Google Maps JavaScript API on initialization:- Checks if Google Maps is already loaded
- Avoids duplicate script tags
- Loads the script with the Places library
- Displays a loading spinner until the map is ready
- Handles script loading errors gracefully
Responsive Sizing
The component uses Angular’s@HostBinding to dynamically apply width and height styles to the host element, ensuring proper layout and responsiveness.
Usage Examples
Basic Usage
Custom Dimensions with String Values
Custom Dimensions with Numeric Values
500 becomes '500px').
Responsive Layout
Dynamic Sizing from Component
Template Structure
The component renders:- A loading spinner with “Cargando mapa…” text while the Google Maps script loads
- The Google Maps component once the script is ready
Styling
The component includes default styles:Environment Configuration
Ensure your environment file includes the Google Maps API key:Loading States
The component provides visual feedback during the loading process:Internal property that tracks whether the Google Maps script has been loaded successfully
isGoogleMapsLoaded is false, the component displays:
- An Ionic spinner with the “crescent” animation
- Loading text: “Cargando mapa…”
Implementation Details
Script Loading Strategy
The component implements a robust script loading strategy:- Check for existing Google Maps: Verifies if
google.mapsis already available - Check for existing script tag: Looks for existing Google Maps script tags in the DOM
- Create script element: If needed, creates a new script tag with:
- API key from environment
- Places library enabled
- Async and defer attributes for optimal loading
- Event handlers: Attaches load and error event handlers
Size Normalization
The component includes a privatenormalize() method that:
- Converts numeric values to pixel strings (e.g.,
400→'400px') - Passes through string values unchanged
- Ensures consistent CSS value formatting
Source Code Reference
See the implementation in:- Component:
src/app/components/base-map/base-map.component.ts - Template:
src/app/components/base-map/base-map.component.html - Styles:
src/app/components/base-map/base-map.component.scss