React components and hooks for Google Maps with gmaps-kit - useGeocoding, useDirections, usePlaces, useMarkers, useStreetView and more
npm install @gmaps-kit/reactReact components and hooks for Google Maps with gmaps-kit - useGeocoding, useDirections, usePlaces, useMarkers, useStreetView and more.


Try it out: https://demo-app-rouge-five.vercel.app/
See all React components and hooks in action!
- βοΈ React Hooks - Easy-to-use hooks for Google Maps functionality
- πΊοΈ Map Component - Ready-to-use Map component
- π Geocoding Hooks - Convert addresses to coordinates
- π§ Directions Hooks - Get and display directions
- π’ Places Hooks - Search and manage places
- π Marker Components - Add markers with React components
- π£οΈ Street View Hooks - Integrate Street View
- π Autocomplete Hooks - Add place autocomplete
- π― TypeScript - Full TypeScript support
- β‘ Lightweight - Optimized bundle size (~13KB)
- π React 16.8+ - Compatible with modern React
``bash`
npm install @gmaps-kit/react @gmaps-kit/core
`tsx
import { useGoogleMaps } from '@gmaps-kit/react';
function App() {
const { isLoaded, load } = useGoogleMaps({
apiKey: 'YOUR_API_KEY',
libraries: ['places', 'geometry'],
});
if (!isLoaded) {
return
return
}
`
`tsx
import { Map } from '@gmaps-kit/react';
function MyMap() {
return (
id="my-map"
center={{ lat: 40.7128, lng: -74.006 }}
zoom={10}
style={{ height: '400px', width: '100%' }}
/>
);
}
`
`tsx
import { Map, Marker } from '@gmaps-kit/react';
import { useMap } from '@gmaps-kit/react';
function MapWithMarkers() {
const { mapInstance } = useMap('my-map', {
center: { lat: 40.7128, lng: -74.006 },
zoom: 10,
});
return (
);
}
`
Initialize Google Maps API:
`tsx
import { useGoogleMaps } from '@gmaps-kit/react';
function App() {
const { isLoaded, isLoading, error, load } = useGoogleMaps({
apiKey: 'YOUR_API_KEY',
libraries: ['places'],
onLoad: () => console.log('Maps loaded!'),
onError: (error) => console.error('Maps error:', error),
});
if (isLoading) return
return
}
`
Convert addresses to coordinates:
`tsx
import { useGeocoding } from '@gmaps-kit/react';
function GeocodingExample() {
const { geocode, reverseGeocode, results, loading, error } = useGeocoding();
const handleGeocode = () => {
geocode('New York City');
};
const handleReverseGeocode = () => {
reverseGeocode({ lat: 40.7128, lng: -74.006 });
};
return (
Loading...
}Error: {error.message}
}{JSON.stringify(results, null, 2)}}$3
Get and display directions:
`tsx
import { useDirections } from '@gmaps-kit/react';function DirectionsExample() {
const { getDirections, renderDirections, clearDirections, result, loading } =
useDirections();
const handleGetDirections = () => {
getDirections({
origin: 'New York, NY',
destination: 'Boston, MA',
travelMode: 'DRIVING',
});
};
return (
{loading && Loading directions...
}
{result && Directions loaded!
}
);
}
`$3
Search and manage places with the legacy Places API:
`tsx
import { usePlaces } from '@gmaps-kit/react';function PlacesExample() {
const {
textSearch,
nearbySearch,
placeDetails,
autocomplete,
results,
loading,
} = usePlaces();
const handleSearch = () => {
textSearch({
query: 'restaurants in New York',
location: { lat: 40.7128, lng: -74.006 },
radius: 1000,
});
};
return (
{loading && Searching...
}
{results && (
{results.map((place, index) => (
- {place.name}
))}
)}
);
}
`$3
Search and manage places with the new Places API (New) - better CORS support, enhanced data, and modern features:
`tsx
import { usePlacesNew } from '@gmaps-kit/react';function PlacesNewExample() {
const {
textSearch,
nearbySearch,
placeDetails,
autocomplete,
getPhoto,
buildPhotoUrl,
isLoading,
error,
} = usePlacesNew({
apiKey: 'YOUR_API_KEY',
// No baseUrl needed - direct API calls with better CORS support!
});
const handleTextSearch = async () => {
try {
const results = await textSearch({
textQuery: 'restaurants in New York',
locationBias: {
circle: {
center: { latitude: 40.7128, longitude: -74.006 },
radius: 1000,
},
},
maxResultCount: 10,
minRating: 4.0,
});
console.log('Text search results:', results);
} catch (error) {
console.error('Search failed:', error);
}
};
const handleNearbySearch = async () => {
try {
const results = await nearbySearch({
includedTypes: ['restaurant', 'cafe'],
locationRestriction: {
circle: {
center: { latitude: 40.7128, longitude: -74.006 },
radius: 1000,
},
},
maxResultCount: 10,
minRating: 4.0,
});
console.log('Nearby search results:', results);
} catch (error) {
console.error('Search failed:', error);
}
};
const handlePlaceDetails = async () => {
try {
const details = await placeDetails({
placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4',
});
console.log('Place details:', details);
} catch (error) {
console.error('Details failed:', error);
}
};
const handleAutocomplete = async () => {
try {
const results = await autocomplete({
input: 'restaurants in',
locationBias: {
circle: {
center: { latitude: 40.7128, longitude: -74.006 },
radius: 1000,
},
},
});
console.log('Autocomplete results:', results);
} catch (error) {
console.error('Autocomplete failed:', error);
}
};
return (
{isLoading &&
Searching...
}
{error && Error: {error.message}
}
);
}
`$3
| Feature | Legacy API | New API (New) |
| ------------------ | ----------------- | -------------------------- |
| CORS Support | β Requires proxy | β
Direct browser requests |
| Request Format | Query parameters | JSON body |
| Authentication | API key in URL | API key in header |
| Data Quality | Basic | Enhanced with more fields |
| Error Handling | Limited | Comprehensive |
| Rate Limiting | Basic | Advanced |
$3
Manage map markers:
`tsx
import { useMarkers } from '@gmaps-kit/react';function MarkersExample({ mapInstance }) {
const { addMarker, removeMarker, updateMarker, markers, clearMarkers } =
useMarkers();
const handleAddMarker = () => {
addMarker({
position: { lat: 40.7128, lng: -74.006 },
title: 'New Marker',
});
};
return (
Markers: {markers.length}
);
}
`$3
Integrate Street View:
`tsx
import { useStreetView } from '@gmaps-kit/react';function StreetViewExample() {
const { createPanorama, setPosition, setPov, setVisible, isVisible } =
useStreetView();
const handleShowStreetView = () => {
createPanorama('street-view-container', {
position: { lat: 40.7128, lng: -74.006 },
pov: { heading: 0, pitch: 0 },
});
setVisible(true);
};
return (
);
}
`Components
$3
Main map component:
`tsx
import { Map } from '@gmaps-kit/react'; id="my-map"
center={{ lat: 40.7128, lng: -74.006 }}
zoom={10}
style={{ height: '400px', width: '100%' }}
className="my-map-class"
onMapReady={(map) => console.log('Map ready!', map)}
>
{/ Child components like markers /}
;
`$3
Add markers to the map:
`tsx
import { Marker } from '@gmaps-kit/react'; mapInstance={mapInstance}
position={{ lat: 40.7128, lng: -74.006 }}
title="My Marker"
draggable={true}
onMarkerCreated={(marker) => console.log('Marker created', marker)}
/>;
`$3
Display info windows:
`tsx
import { InfoWindow } from '@gmaps-kit/react'; mapInstance={mapInstance}
position={{ lat: 40.7128, lng: -74.006 }}
content="
Hello World!
"
onClose={() => console.log('Info window closed')}
/>;
`TypeScript Support
Full TypeScript support with comprehensive type definitions:
`tsx
import type {
UseGeocodingReturn,
UseDirectionsReturn,
MapProps,
MarkerProps,
} from '@gmaps-kit/react';const MyComponent: React.FC = () => {
const geocoding: UseGeocodingReturn = useGeocoding();
// TypeScript will provide full intellisense
};
``#### useGoogleMaps
Load Google Maps SDK and manage loading state.
#### useMap
Create and manage Google Map instances.
#### useMarkers
Manage markers on the map.
#### useMapEvents
Handle map events (click, drag, etc.).
#### useGeocoding
Client-side geocoding with JavaScript SDK.
#### useGeocodingService
Server-side geocoding with REST API.
#### usePlaces
Legacy Places API integration.
#### usePlacesNew
π New Places API (New) with enhanced CORS support.
#### useDirections
Calculate and display routes.
#### useBicycling
Bicycling layer and routing.
#### useTraffic
Traffic layer and data.
#### useTransit
Transit layer and routing.
#### useClustering
Marker clustering for performance.
#### useDistanceMatrix
Calculate distances and travel times.
#### useElevation
Elevation data and terrain.
#### useGeometry
Geometric calculations (distance, bearing, area).
#### useHeatmap
Heatmap visualization.
#### useInfoWindows
Manage multiple InfoWindows.
#### useMaxZoom
Maximum zoom level management.
#### useStreetView
Street View panorama management.
- React 16.8+ (hooks support)
- @gmaps-kit/core
- Google Maps API key
- Chrome 60+
- Firefox 55+
- Safari 12+
- Edge 79+
MIT Β© gmaps-kit
Contributions are welcome! Please read our Contributing Guide for details.
- π Documentation
- π Report Issues
- π¬ Discussions