@react-google-maps/api
Version:
React.js Google Maps API integration
89 lines (72 loc) • 1.66 kB
Markdown
required to load a simple map are:
- LoadScript - Loads the Google Maps API script
- GoogleMap - The map component inside which all other components render
The simplest way to get a functional map is:
> ⚠️ Make sure you cache the props passed to `GoogleMap` to avoid re-renders that may harm the performance.
```md
import React, { Component } from 'react';
import { GoogleMap, LoadScript } from '@react-google-maps/api';
const containerStyle = {
width: '400px',
height: '400px'
};
const center = {
lat: -3.745,
lng: -38.523
};
class MyComponents extends Component {
render() {
return (
<LoadScript
googleMapsApiKey="YOUR_API_KEY"
>
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={10}
>
{ /_ Child components, such as markers, info windows, etc. _/ }
<></>
</GoogleMap>
</LoadScript>
)
}
}
```
Or you can also adopt a functional component style:
```md
import React from 'react'
import { GoogleMap, LoadScript } from '@react-google-maps/api';
const containerStyle = {
width: '400px',
height: '400px'
};
const center = {
lat: -3.745,
lng: -38.523
};
function MyComponent() {
return (
<LoadScript
googleMapsApiKey="YOUR_API_KEY"
>
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={10}
>
{ /_ Child components, such as markers, info windows, etc. _/ }
<></>
</GoogleMap>
</LoadScript>
)
}
export default React.memo(MyComponent)
```
```bash
npm install --save @react-google-maps/api
pnpm install @react-google-maps/api
```
The two basic components