Why Use @react-google-maps/api?
-
Works with modern functional components
-
Supports lazy loading (loads only when needed)
- Compatible with the latest Google Maps version (v3.55 and above)
- Makes map rendering simple and fast
Step 1: Install the Required Package
npm install @react-google-maps/api
Step 2: Get Your Google Maps API Key
-
Maps JavaScript API
-
Places API (for location search or autocomplete)
Step 3: Create the Map Component
// components/MyMap.tsx
import React from 'react';
import { GoogleMap, useLoadScript } from '@react-google-maps/api';
const containerStyle = {
width: '100%',
height: '500px',
};
const center = {
lat: 37.7749, // Example: San Francisco
lng: -122.4194,
};
const MyMap = () => {
const { isLoaded } = useLoadScript({
googleMapsApiKey: 'YOUR_API_KEY', // Replace with your real key
version: 'weekly',
libraries: ['marker'], // Needed for new marker features
});
if (!isLoaded) return <div>Loading...</div>;
return (
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={12}
onLoad={(map) => {
const { AdvancedMarkerElement } = window.google.maps;
new AdvancedMarkerElement({
map,
position: center,
title: 'You are here!',
});
}}
/>
);
};
export default MyMap;
This code:
-
Loads the map only when it is ready
-
Displays a marker on a selected location
- Uses the AdvancedMarkerElement for better design and performance
Step 4: Use the Map in Your App
Now you can import and use the map component in your main app.
// App.tsx
import React from 'react';
import MyMap from './components/MyMap';
function App() {
return (
<div>
<h2>My React App with Google Maps</h2>
<MyMap />
</div>
);
}
export default App;
This will render the map when your app loads.
Customization Ideas
-
Custom Icons: Replace the default marker with your own image
-
InfoWindow: Show details when users click on a location
- Autocomplete: Let users search for locations easily
- DirectionsRenderer: Show driving, walking, or transit routes
-
User Location: Show real-time location of the user using browser geolocation
Pro Tips for Better Security and Performance
-
Use .dockerignore and .env files to hide your API keys
-
Restrict your API key by domain and enabled services
- Monitor your API usage to avoid high bills
- Use multi-stage builds in Docker to keep images small
-
Use environment variables for API keys instead of writing them in code
-
Add error boundaries (like react-error-boundary) to avoid app crashes
Google Maps & React in 2025 – Key Stats
-
Over 90% of map-based apps will use Google Maps in 2025
-
Apps built with React + Google Maps are 30% faster to develop
- Tools like @react-google-maps/api have cut setup time by half
- Google Maps offers real-time features, perfect for logistics, food delivery, travel, etc.
Final Thoughts
-
Set up your API key safely
-
Build fast-loading map features
- Add search, routes, and custom designs
- Deploy to production with ease
Get in touch for a FREE consultation today! Let’s bring your idea to life with maps that work for your users and your business.
Vikas Mishra
A highly skilled Angular & React Js Developer. Committed to delivering efficient, high-quality solutions by simplifying complex projects with technical expertise and innovative thinking.
Reply