Docs / Setup / React / Google Maps

Google Maps setup

How to use Google Maps on the Web through MapConductor. The Google Maps JavaScript API is loaded dynamically, so there is no script tag to write — but 2D and 3D are separate view components.

PACKAGE
react-for-googlemaps
API KEY
Required
CSS
None
REACT NATIVE
Available
Same provider, other platformsAndroidiOS

Before you start

Node.js 18 or newer
A Google Cloud project with the Maps JavaScript API enabled
An API key restricted by HTTP referrer
STEP 01

Get an API key

  1. Enable the Maps JavaScript API in the Google Cloud Console.
  2. Create an API key under Credentials.
  3. Restrict the key by HTTP referrer to the domains you serve from.
CAUTION
Feed the key from a build-time environment variable (import.meta.env.VITE_* under Vite) and keep it out of source control. It reaches the browser either way, so always restrict it by referrer or domain so exposure is harmless.
STEP 02

Install

The Google Maps JavaScript API is loaded dynamically via @googlemaps/js-api-loader, which ships bundled with the package. Nothing else to install, and no CSS to import.

terminalnpm
npm install @mapconductor/js-sdk-core @mapconductor/js-sdk-react \
            @mapconductor/react-for-googlemaps
STEP 03

Pass the key to the state hook

The key goes to the state hook, not to the view component's props — props tend to end up in test logs and snapshots.

HelloMap.tsxTypeScript · React
const mapState = useGoogleMapViewState({
  id: 'web-google-map',
  apiKey: import.meta.env.VITE_GOOGLE_MAPS_API_KEY,
  mapDesignType: GoogleMapDesign.Normal,
  cameraPosition: createMapCameraPosition({ position: TOKYO, zoom: 12 }),
});
There is no dedicated “missing key” error — it surfaces as a failed SDK load, so suspect the key first.
STEP 04

Choose between 2D and 3D

Google Maps has two independent stacks, and MapConductor exposes one view for each; they share a state object. Setting mapId also switches the 2D provider's markers to AdvancedMarkerElement — without one it falls back to the legacy renderer.

HelloMap.tsxTypeScript · React
// Classic 2D map (google.maps.Map)
<GoogleMapView2D state={mapState} mapId="YOUR_MAP_ID" />

// True 3D camera (google.maps.maps3d.Map3DElement)
<GoogleMapView state={mapState} mapId="YOUR_MAP_ID" version="alpha" />
CAUTION
The 3D view (GoogleMapView) accepts only Normal, Hybrid and Satellite — passing Terrain or None throws at runtime. It also needs version="alpha" because it uses the maps3d library.

VERIFY

What you should see

A map centred on Tokyo with a single marker means you are done.

HelloMap.tsxTypeScript · React
import { useState } from 'react';
import { createGeoPoint, createMapCameraPosition, createMarkerState } from '@mapconductor/js-sdk-core';
import { Marker } from '@mapconductor/js-sdk-react';
import { GoogleMapDesign, GoogleMapView2D, useGoogleMapViewState } from '@mapconductor/react-for-googlemaps';

const TOKYO = createGeoPoint({ latitude: 35.6812, longitude: 139.7671 });

export function HelloMap() {
  const mapState = useGoogleMapViewState({
    id: 'web-google-map',
    apiKey: import.meta.env.VITE_GOOGLE_MAPS_API_KEY,
    mapDesignType: GoogleMapDesign.Normal,
    cameraPosition: createMapCameraPosition({ position: TOKYO, zoom: 12 }),
  });
  const [markerState] = useState(() => createMarkerState({ id: 'tokyo', position: TOKYO }));

  return (
    <GoogleMapView2D state={mapState} style={{ height: 480 }}>
      <Marker state={markerState} />
    </GoogleMapView2D>
  );
}

State object

What useGoogleMapViewState accepts.

signatureTypeScript
useGoogleMapViewState({
  id?: string;
  apiKey?: string;                     // required in practice
  mapId?: string;                      // Advanced Markers / cloud styling
  mapDesignType?: GoogleMapDesignType; // default GoogleMapDesign.Normal
  cameraPosition?: MapCameraPosition;  // default MapCameraPosition.Default
})

Map designs

GoogleMapDesign mirrors Google's map type ids.

GoogleMapDesign
Notes
Normal
roadmap — valid in 3D too
Satellite
Satellite imagery — valid in 3D too
Hybrid
Satellite with labels — valid in 3D too
Terrain
Terrain — 2D only
None
No basemap tiles — 2D only

Troubleshooting

The map does not appear

  • Check the key reaches the state hook and that the environment variable resolved.
  • Verify the key's referrer restriction matches the domain you serve from.

The 3D view throws

  • Check you are not passing Terrain or None — the 3D view accepts only Normal, Hybrid and Satellite.
  • Confirm you passed version="alpha".

The map initialises twice in development

  • React StrictMode double-invokes effects. Every provider detects this and discards the aborted run, so you do not need to turn StrictMode off.

Next

The 3D view rescales the camera range on viewport changes, so zoom looks consistent across window sizes.