Building a Google Maps Application with React and TypeScript
· 阅读需 3 分钟
Prerequisites
- Node.js installed (version 14 or higher).
- npm or yarn for package management.
- A Google Cloud Platform (GCP) account to obtain the API Key and set up the Map ID.
Project Setup
-
Create a React Application:
npx create-react-app google-maps-app --template typescriptcd google-maps-app -
Install Required Dependencies:
npm install @react-google-maps/apinpm install react-hook-form -
Setup Google Cloud Platform:
- Enable the Maps JavaScript API:
- Go to the Google Cloud Console.
- Create a new project or select an existing one.
- Navigate to
APIs & Services>Libraryand enable theMaps JavaScript API.
- Generate an API Key:
- Go to
APIs & Services>Credentialsand create an API Key. - Restrict the API Key to ensure security by specifying HTTP referrers (your domain or
localhost).
- Go to
- Create a Map ID:
- Go to
Google Maps Platform>Maps>Map Management. - Create a new Map ID and configure it with desired styles and settings.
- Go to
- Enable the Maps JavaScript API:
Project Structure
google-maps-app/
├── public/
├── src/
│ ├── components/
│ │ ├── MapContainer.tsx
│ │ ├── SearchBox.tsx
│ ├── App.tsx
│ ├── index.tsx
│ ├── types/
│ │ ├── index.d.ts
│ ├── utils/
│ ├── mapUtils.ts
Coding the Application
Step 1: Setup TypeScript Types
Create src/types/index.d.ts to define types for the Google Maps objects:
declare global {
interface Window {
google: any;
}
}
export interface MarkerData {
position: google.maps.LatLngLiteral;
title: string;
}
Step 2: Utility Functions
Create src/utils/mapUtils.ts to define helper functions:
export const loadGoogleMapsScript = (apiKey: string): Promise<void> => {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = `https://maps.googleapis.com/maps/api/js?key=${apiKey}&libraries=places`;
script.async = true;
script.defer = true;
script.onload = () => resolve();
script.onerror = (e) => reject(e);
document.head.appendChild(script);
});
};
Step 3: Map Container
Create src/components/MapContainer.tsx:
import React, { useEffect, useRef, useState } from 'react';
import { MarkerData } from '../types';
type MapContainerProps = {
apiKey: string;
mapId: string;
};
const MapContainer: React.FC<MapContainerProps> = ({ apiKey, mapId }) => {
const mapRef = useRef<HTMLDivElement>(null);
const [map, setMap] = useState<google.maps.Map | null>(null);
const [markers, setMarkers] = useState<google.maps.Marker[]>([]);
useEffect(() => {
const initMap = async () => {
try {
await loadGoogleMapsScript(apiKey);
if (mapRef.current) {
const mapInstance = new google.maps.Map(mapRef.current, {
center: { lat: 37.7749, lng: -122.4194 },
zoom: 12,
mapId: mapId,
});
setMap(mapInstance);
}
} catch (error) {
console.error('Failed to load Google Maps:', error);
}
};
initMap();
}, [apiKey, mapId]);
const addMarker = (markerData: MarkerData) => {
if (map) {
const marker = new google.maps.marker.AdvancedMarkerElement({
map,
position: markerData.position,
title: markerData.title,
});
setMarkers((prev) => [...prev, marker]);
}
};
return <div ref={mapRef} style={{ width: '100%', height: '500px' }} />;
};
export default MapContainer;
Step 4: Search Box
Create src/components/SearchBox.tsx:
import React, { useEffect, useRef } from 'react';
type SearchBoxProps = {
onPlaceSelected: (place: google.maps.places.PlaceResult) => void;
};
const SearchBox: React.FC<SearchBoxProps> = ({ onPlaceSelected }) => {
const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (inputRef.current) {
const autocomplete = new google.maps.places.Autocomplete(inputRef.current);
autocomplete.addListener('place_changed', () => {
const place = autocomplete.getPlace();
if (place) {
onPlaceSelected(place);
}
});
}
}, []);
return <input ref={inputRef} type="text" placeholder="Search places..." />;
};
export default SearchBox;
Step 5: Combine Components
Update src/App.tsx:
import React, { useState } from 'react';
import MapContainer from './components/MapContainer';
import SearchBox from './components/SearchBox';
import { MarkerData } from './types';
const App: React.FC = () => {
const [markers, setMarkers] = useState<MarkerData[]>([]);
const apiKey = 'YOUR_GOOGLE_MAPS_API_KEY';
const mapId = 'YOUR_MAP_ID';
const handlePlaceSelected = (place: google.maps.places.PlaceResult) => {
if (place.geometry?.location) {
const markerData = {
position: {
lat: place.geometry.location.lat(),
lng: place.geometry.location.lng(),
},
title: place.name || '',
};
setMarkers((prev) => [...prev, markerData]);
}
};
return (
<div>
<SearchBox onPlaceSelected={handlePlaceSelected} />
<MapContainer apiKey={apiKey} mapId={mapId} />
</div>
);
};
export default App;
Running the Application
- Replace
YOUR_GOOGLE_MAPS_API_KEYandYOUR_MAP_IDinApp.tsxwith your actual values. - Start the application:
npm start
- Open
http://localhost:3000in your browser.