Skip to content

GoogleMap

<GoogleMap /> component is used for display Map instance.

Basic Example

function App() {
return (
<Suspense fallback={...}>
<GoogleMapApiLoader
apiKey='YOUR_API_KEY'
suspense
>
<GoogleMap
// map container element props
className='h-[400px]'
containerProps={{ id: 'my-map' }}
// map options
zoom={16}
center={{ lat: 37.5111158, lng: 127.098167 }}
mapOptions={{
mapTypeId: 'satellite',
}}
/>
</GoogleMapApiLoader>
</Suspense>
);
}

Props

PropsTypeDescription
styleHTMLAttributes<HTMLDivElement>['style']style props for map container element
classNameHTMLAttributes<HTMLDivElement>['className']className props for map container element
containerPropsHTMLAttributes<HTMLDivElement>props object for map container element
initialCentergoogle.maps.LatLng | google.maps.LatLngLiteralinitial Map center.
initialZoomnumberinitial Map zoom level.
zoomnumbermap zoom level.
centergoogle.maps.LatLng | google.maps.LatLngLiteralmap center coordiante.
mapOptionsgoogle.maps.MapOptionsoptions for map. Please refer to the official documentation.
onLoad(map: google.maps.Map) => voidcallback for map creating success
onBoundsChanged(map: google.maps.Map) => voidcallback for bounds_changed event. Please refer to the official documentation.
onCenterChanged(map: google.maps.Map) => voidcallback for center_changed event. Please refer to the official documentation.
onClick(map: google.maps.Map, event: google.maps.MapMouseEvent | google.maps.IconMouseEvent) => voidcallback for click event. Please refer to the official documentation.
onContextmenu(map: google.maps.Map, event: google.maps.MapMouseEvent) => voidcallback for contextmenu event. Please refer to the official documentation.
onDblclick(map: google.maps.Map, event: google.maps.MapMouseEvent) => voidcallback for dblclick event. Please refer to the official documentation.
onDrag(map: google.maps.Map) => voidcallback for drag event. Please refer to the official documentation.
onDragEnd(map: google.maps.Map) => voidcallback for dragend event. Please refer to the official documentation.
onDragStart(map: google.maps.Map) => voidcallback for dragstart event. Please refer to the official documentation.
onHeadingChanged(map: google.maps.Map) => voidcallback for heading_changed event. Please refer to the official documentation.
onIdle(map: google.maps.Map) => voidcallback for idle event. Please refer to the official documentation.
onIsFractionalZoomEnabledChanged(map: google.maps.Map) => voidcallback for isfractionalzoomenabled_changed event. Please refer to the official documentation.
onMapCapabilitiesChanged(map: google.maps.Map) => voidcallback for mapcapabilities_changed event. Please refer to the official documentation.
onMapTypeIdChanged(map: google.maps.Map) => voidcallback for maptypeid_changed event. Please refer to the official documentation.
onMouseMove(map: google.maps.Map, event: google.maps.MapMouseEvent) => voidcallback for mousemove event. Please refer to the official documentation.
onMouseOut(map: google.maps.Map, event: google.maps.MapMouseEvent) => voidcallback for mouseout event. Please refer to the official documentation.
onMouseOver(map: google.maps.Map, event: google.maps.MapMouseEvent) => voidcallback for mouseover event. Please refer to the official documentation.
onProjectionChanged(map: google.maps.Map) => voidcallback for projection_changed event. Please refer to the official documentation.
onRenderingTypeChanged(map: google.maps.Map) => voidcallback for renderingtype_changed event. Please refer to the official documentation.
onTilesLoaded(map: google.maps.Map) => voidcallback for tilesloaded event. Please refer to the official documentation.
onTiltChanged(map: google.maps.Map) => voidcallback for tilt_changed event. Please refer to the official documentation.
onZoomChanged(map: google.maps.Map) => voidcallback for zoom_changed event. Please refer to the official documentation.

Access the Map Instance

You can access the map instance through three methods.

    1. Event Callbacks (onLoad and others)
    1. ref
    1. useMapContext
const [map, setMap] = useState<google.maps.Map | null(null);
<GoogleMap
onLoad={(map) => setMap(map)}
/>
/// or
const mapRef = useRef<google.maps.Map>(null);
<GoogleMap
ref={mapRef}
/>
/// or
<GooelMap>
<MyComponent />
</GooelMap>
function MyComponent() {
const map = useMapContext();
}