Overview
TheMap component provides an interactive Google Maps interface that allows users to select coordinates by clicking on the map. It manages map initialization, marker rendering, and click event handling.
Props
Callback function invoked when the user clicks on the map. Receives a coordinate object with
lat and lng properties.Array of marker objects to display on the map. Currently used for component update optimization.
Instance Variables
The Google Maps instance created during component initialization
Array to store Google Maps marker instances (initialized as empty array)
Methods
showMap
Initializes the Google Maps instance when the component mounts. This method is called via a ref callback.The DOM element that will contain the map
- Creates a new Google Maps instance in the provided DOM element
- Sets initial zoom level to 12
- Centers the map on Buenos Aires (-34.6037, -58.3816)
- Adds click event listener to capture user-selected coordinates
- When user clicks the map, extracts latitude and longitude
- Converts Google Maps LatLng object to plain coordinate object
- Calls the
selectMapPointcallback with the coordinates
shouldComponentUpdate
Prevents unnecessary re-renders of the map component.- Always returns
falseto prevent re-renders - The map is managed imperatively rather than declaratively
- Markers would be updated in this lifecycle method (implementation pending)
Render Method
- Creates a 500x500 pixel container div
- Uses ref callback to initialize the map when the element is mounted
- Inline styles set the map dimensions
Creating Markers
While the Map component manages the container, markers are typically created externally. Here’s how markers are created in the application:Coordinate object with
lat and lng propertiesThe map instance to display the marker on
Whether the marker can be dragged. Typically
false for route stops.Text label displayed on the marker
Tooltip text shown on marker hover
Directions Service Integration
The application uses Google Maps Directions Service for route calculation. This is typically handled by the parent App component:- origin: First point in the route
- destination: Last point in the route
- waypoints: All intermediate points (not including first and last)
- optimizeWaypoints:
true- Google Maps optimizes the order - travelMode:
'DRIVING'- Calculates driving directions - suppressMarkers:
true- Uses custom markers instead of defaults
Usage Example
Map Styling
The map container is rendered with fixed dimensions:File Location
src/client/Map.jsx