Skip to main content

GoogleMap

The GoogleMap component is the main component for rendering a Google Map in your React application. It provides a wrapper around the Google Maps JavaScript API and manages the map instance lifecycle.

Import

import { GoogleMap } from '@react-google-maps/api'

TypeScript Signature

function GoogleMap(props: GoogleMapProps): JSX.Element

type GoogleMapProps = {
  children?: ReactNode | undefined
  id?: string | undefined
  mapContainerStyle?: CSSProperties | undefined
  mapContainerClassName?: string | undefined
  options?: google.maps.MapOptions | undefined
  extraMapTypes?: google.maps.MapType[] | undefined
  center?: google.maps.LatLng | google.maps.LatLngLiteral | undefined
  clickableIcons?: boolean | undefined
  heading?: number | undefined
  mapTypeId?: string | undefined
  streetView?: google.maps.StreetViewPanorama | undefined
  tilt?: number | undefined
  zoom?: number | undefined
  onClick?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onDblClick?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onDrag?: (() => void) | undefined
  onDragEnd?: (() => void) | undefined
  onDragStart?: (() => void) | undefined
  onMouseMove?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onMouseOut?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onMouseOver?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onMouseDown?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onMouseUp?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onRightClick?: ((e: google.maps.MapMouseEvent) => void) | undefined
  onMapTypeIdChanged?: (() => void) | undefined
  onTilesLoaded?: (() => void) | undefined
  onBoundsChanged?: (() => void) | undefined
  onCenterChanged?: (() => void) | undefined
  onHeadingChanged?: (() => void) | undefined
  onIdle?: (() => void) | undefined
  onProjectionChanged?: (() => void) | undefined
  onResize?: (() => void) | undefined
  onTiltChanged?: (() => void) | undefined
  onZoomChanged?: (() => void) | undefined
  onLoad?: ((map: google.maps.Map) => void | Promise<void>) | undefined
  onUnmount?: ((map: google.maps.Map) => void | Promise<void>) | undefined
}

Props

Container Props

id
string
Optional ID attribute for the map container div element.
mapContainerStyle
CSSProperties
Inline styles for the map container. Use this to set width and height.
mapContainerStyle={{ width: '100%', height: '400px' }}
mapContainerClassName
string
CSS class name for the map container div element.
children
ReactNode
Child components to render within the map context (Markers, Polylines, etc.).

Map Configuration Props

options
google.maps.MapOptions
Full Google Maps MapOptions object. Provides complete control over map configuration.See Google Maps MapOptions documentation for all available options.
center
google.maps.LatLng | google.maps.LatLngLiteral
The initial map center position. Can be a LatLng object or literal.
center={{ lat: 40.7128, lng: -74.0060 }}
zoom
number
The initial map zoom level. Valid values are 0 (world view) to 22+ (street level).
zoom={10}
mapTypeId
string
The map type to display. Options include:
  • 'roadmap' - Default road map view
  • 'satellite' - Satellite imagery
  • 'hybrid' - Satellite with road overlay
  • 'terrain' - Terrain with physical features
heading
number
The heading for aerial imagery in degrees. Only applies to satellite and hybrid map types.
tilt
number
The angle of incidence for aerial imagery. Valid values are 0 (top-down) and 45 (perspective).
clickableIcons
boolean
Whether POI (Point of Interest) icons are clickable.
streetView
google.maps.StreetViewPanorama
A StreetViewPanorama instance to bind to the map.
extraMapTypes
google.maps.MapType[]
Additional custom map types to register with the map.

Mouse Event Handlers

onClick
(e: google.maps.MapMouseEvent) => void
Called when the map is clicked.
onClick={(e) => {
  console.log('Clicked at:', e.latLng?.lat(), e.latLng?.lng())
}}
onDblClick
(e: google.maps.MapMouseEvent) => void
Called when the map is double-clicked.
onRightClick
(e: google.maps.MapMouseEvent) => void
Called when the map is right-clicked.
onMouseMove
(e: google.maps.MapMouseEvent) => void
Called when the mouse moves over the map.
onMouseOver
(e: google.maps.MapMouseEvent) => void
Called when the mouse enters the map.
onMouseOut
(e: google.maps.MapMouseEvent) => void
Called when the mouse leaves the map.
onMouseDown
(e: google.maps.MapMouseEvent) => void
Called when a mouse button is pressed on the map.
onMouseUp
(e: google.maps.MapMouseEvent) => void
Called when a mouse button is released on the map.

Drag Event Handlers

onDrag
() => void
Called repeatedly while the user drags the map.
onDragStart
() => void
Called when the user starts dragging the map.
onDragEnd
() => void
Called when the user stops dragging the map.

Map State Change Handlers

onBoundsChanged
() => void
Called when the map viewport bounds change.
onCenterChanged
() => void
Called when the map center changes.
onZoomChanged
() => void
Called when the map zoom level changes.
onHeadingChanged
() => void
Called when the map heading changes.
onTiltChanged
() => void
Called when the map tilt changes.
onMapTypeIdChanged
() => void
Called when the map type changes.
onProjectionChanged
() => void
Called when the map projection changes.

Lifecycle Event Handlers

onLoad
(map: google.maps.Map) => void | Promise<void>
Called when the map instance is loaded. Receives the map instance as a parameter.
onLoad={(map) => {
  console.log('Map loaded:', map)
  // Store map instance, add custom controls, etc.
}}
onUnmount
(map: google.maps.Map) => void | Promise<void>
Called when the map instance is about to be unmounted. Receives the map instance as a parameter.
onUnmount={(map) => {
  console.log('Map unmounting:', map)
  // Cleanup, remove listeners, etc.
}}
onIdle
() => void
Called when the map becomes idle after panning or zooming.
onResize
() => void
Called when the map div changes size.
onTilesLoaded
() => void
Called when the map tiles have finished loading.

Usage Example

import { GoogleMap, LoadScript } from '@react-google-maps/api'

const containerStyle = {
  width: '100%',
  height: '400px'
}

const center = {
  lat: 40.7128,
  lng: -74.0060
}

function MapComponent() {
  const [map, setMap] = useState<google.maps.Map | null>(null)

  const onLoad = useCallback((map: google.maps.Map) => {
    // Store map instance
    setMap(map)
    
    // Optional: Fit bounds, add controls, etc.
    const bounds = new google.maps.LatLngBounds(center)
    map.fitBounds(bounds)
  }, [])

  const onUnmount = useCallback(() => {
    setMap(null)
  }, [])

  const onClick = useCallback((e: google.maps.MapMouseEvent) => {
    console.log('Clicked at:', e.latLng?.toJSON())
  }, [])

  return (
    <LoadScript googleMapsApiKey="YOUR_API_KEY">
      <GoogleMap
        mapContainerStyle={containerStyle}
        center={center}
        zoom={10}
        onLoad={onLoad}
        onUnmount={onUnmount}
        onClick={onClick}
        options={{
          streetViewControl: false,
          mapTypeControl: true,
        }}
      >
        {/* Add child components like Markers, Polylines, etc. */}
      </GoogleMap>
    </LoadScript>
  )
}

Advanced Usage

Custom Map Options

<GoogleMap
  center={center}
  zoom={10}
  options={{
    disableDefaultUI: true,
    zoomControl: true,
    mapTypeControl: false,
    scaleControl: true,
    streetViewControl: false,
    rotateControl: false,
    fullscreenControl: true,
    styles: [
      {
        featureType: 'water',
        elementType: 'geometry',
        stylers: [{ color: '#193341' }]
      }
    ]
  }}
/>

Accessing Map Instance

const mapRef = useRef<google.maps.Map | null>(null)

const handleLoad = (map: google.maps.Map) => {
  mapRef.current = map
}

const panToLocation = (lat: number, lng: number) => {
  if (mapRef.current) {
    mapRef.current.panTo({ lat, lng })
  }
}

Notes

  • The GoogleMap component must be wrapped in a LoadScript or used with useLoadScript hook
  • Child components have access to the map instance through React Context
  • The map container requires explicit dimensions (width and height)
  • Event handlers are automatically cleaned up on unmount
  • The component is memoized for performance optimization

See Also

Build docs developers (and LLMs) love