Skip to content

Repository files navigation

English | 日本語 | Español (Latinoamérica)

@mapconductor/react-for-googlemaps

Google Maps provider for the MapConductor React SDK. Renders a Google Map through MapConductor's provider-independent camera, marker, and overlay API, so the same application code can also run on MapLibre, Mapbox, Leaflet, OpenLayers, ArcGIS, Cesium, or HERE.

Installation

npm install @mapconductor/react-for-googlemaps

@mapconductor/js-sdk-core and @mapconductor/js-sdk-react (used for markers and other shared components) are installed automatically as dependencies. Your code imports from both directly, so with pnpm's strict (isolated) node_modules — or whenever you prefer to declare everything you import — install them explicitly instead:

npm install @mapconductor/react-for-googlemaps @mapconductor/js-sdk-core @mapconductor/js-sdk-react

The Maps JavaScript API is loaded at runtime via @googlemaps/js-api-loader; you only need an API key from the Google Cloud console.

Hello Map tutorial

The simplest possible map app, built with MapConductor + Google Maps: click the marker and a "Hello, MapConductor" bubble pops up. You can build it in the 5 steps below. Google Maps requires an API key, so just add your own Google Maps API key and it works.

Step 1: Create a React project

Create a React + TypeScript project with Vite.

npm create vite@latest hello-map -- --template react-ts
cd hello-map
npm install
npm run dev

Step 2: Install MapConductor (Google Maps)

Install the package needed to show a map. We use Google Maps here, but you can use other map modules too.

npm install @mapconductor/react-for-googlemaps
  • @mapconductor/react-for-googlemaps — components / hooks for Google Maps
  • @mapconductor/js-sdk-react / @mapconductor/js-sdk-core are installed automatically as dependencies.
  • You'll also need an API key from the Google Cloud console, set as the GOOGLE_MAPS_API_KEY environment variable.

Step 3: Show the map

Create the map state with useGoogleMapViewState and render it with <GoogleMapView2D>. Give the outer element a height to make it full-screen.

import{GoogleMapDesign,GoogleMapView2D,useGoogleMapViewState,}from'@mapconductor/react-for-googlemaps';import{createGeoPoint,createMapCameraPosition}from'@mapconductor/js-sdk-core';// Your own key. Read it from your environment however your build tool does// it, and keep it out of source control.constGOOGLE_MAPS_API_KEY='…';constTOKYO=createGeoPoint({latitude: 35.6812,longitude: 139.7671});constINITIAL_CAMERA=createMapCameraPosition({position: TOKYO,zoom: 14});exportdefaultfunctionApp(){constmapViewState=useGoogleMapViewState({apiKey: GOOGLE_MAPS_API_KEY,mapDesignType: GoogleMapDesign.Normal,cameraPosition: INITIAL_CAMERA,});return(<divstyle={{width: '100vw',height: '100vh'}}><GoogleMapView2Dstate={mapViewState}/></div>);}

Use GoogleMapView instead of GoogleMapView2D for the photorealistic 3D map view.

Step 4: Place a marker

Create the marker state with createMarkerState and register it with <Marker>. Write overlays as child elements of the map component.

import{useMemo}from'react';import{createMarkerState}from'@mapconductor/js-sdk-core';import{Marker}from'@mapconductor/js-sdk-react';// ...inside App...constmarker=useMemo(()=>createMarkerState({id: 'hello',position: TOKYO}),[],);// ...inside return...<GoogleMapView2Dstate={mapViewState}><Markerstate={marker}/></GoogleMapView2D>

Step 5: Show an InfoBubble on click

Track the selected state with useState, set it to true in the marker's onClick, and render <InfoBubble> only while selected. This is the finished app.

import{useMemo,useState}from'react';import{GoogleMapDesign,GoogleMapView2D,useGoogleMapViewState,}from'@mapconductor/react-for-googlemaps';import{createGeoPoint,createMapCameraPosition,createMarkerState,}from'@mapconductor/js-sdk-core';import{InfoBubble,Marker}from'@mapconductor/js-sdk-react';// Your own key. Read it from your environment however your build tool does// it, and keep it out of source control.constGOOGLE_MAPS_API_KEY='…';constTOKYO=createGeoPoint({latitude: 35.6812,longitude: 139.7671});constINITIAL_CAMERA=createMapCameraPosition({position: TOKYO,zoom: 14});exportdefaultfunctionApp(){constmapViewState=useGoogleMapViewState({apiKey: GOOGLE_MAPS_API_KEY,mapDesignType: GoogleMapDesign.Normal,cameraPosition: INITIAL_CAMERA,});const[selected,setSelected]=useState(false);constmarker=useMemo(()=>createMarkerState({id: 'hello',position: TOKYO,onClick: ()=>setSelected(true),}),[],);return(<divstyle={{width: '100vw',height: '100vh'}}><GoogleMapView2Dstate={mapViewState}onMapClick={()=>setSelected(false)}><Markerstate={marker}/>{selected&&(<InfoBubblemarker={marker}><divstyle={{padding: '8px 12px',fontWeight: 600}}>
Hello, MapConductor
</div></InfoBubble>)}</GoogleMapView2D></div>);}

Key points

  • Coordinates, cameras and markers are created with js-sdk-core functions (provider-independent).
  • The map component and hooks come from react-for-googlemaps (provider-specific).
  • Write overlays as child elements of the map component.
  • Control show / hide with React useState.

Related packages

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages