Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@react-google-maps/api
Advanced tools
@react-google-maps/api is a library that provides a set of React components for integrating Google Maps into your React applications. It offers a simple and efficient way to use Google Maps features such as markers, info windows, and various map controls.
Basic Map
This code demonstrates how to render a basic Google Map using the @react-google-maps/api package. The map is centered at a specific latitude and longitude and has a defined container style.
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 MyComponent;
Markers
This code demonstrates how to add a marker to the Google Map. The marker is positioned at the center of the map.
import { GoogleMap, LoadScript, Marker } 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}
>
<Marker position={center} />
</GoogleMap>
</LoadScript>
)
}
export default MyComponent;
Info Windows
This code demonstrates how to add an Info Window to a marker on the Google Map. The Info Window appears when the marker is clicked and can be closed by clicking the close button.
import { GoogleMap, LoadScript, Marker, InfoWindow } from '@react-google-maps/api';
import { useState } from 'react';
const containerStyle = {
width: '400px',
height: '400px'
};
const center = {
lat: -3.745,
lng: -38.523
};
function MyComponent() {
const [selected, setSelected] = useState(null);
return (
<LoadScript googleMapsApiKey="YOUR_API_KEY">
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={10}
>
<Marker
position={center}
onClick={() => setSelected(center)}
/>
{selected && (
<InfoWindow
position={center}
onCloseClick={() => setSelected(null)}
>
<div>
<h2>Info Window</h2>
<p>Details about this location.</p>
</div>
</InfoWindow>
)}
</GoogleMap>
</LoadScript>
)
}
export default MyComponent;
google-maps-react is another popular library for integrating Google Maps into React applications. It provides a set of React components similar to @react-google-maps/api but is considered less performant and less actively maintained.
react-google-maps is an older library for Google Maps integration in React. It offers similar functionalities but is no longer actively maintained, and users are encouraged to migrate to @react-google-maps/api for better performance and support.
react-map-gl is a library for integrating Mapbox maps into React applications. While it does not use Google Maps, it offers similar functionalities for map rendering, markers, and controls, making it a good alternative for those who prefer Mapbox.
@react-google-maps/api
This library requires React v16.6 or later. To use the latest features (including hooks) requires React v16.8+. If you need support for earlier versions of React, you should check out react-google-maps
This is complete re-write of the (sadly unmaintained) react-google-maps
library. We thank tomchentw for his great work that made possible.
@react-google-maps/api provides very simple bindings to the google maps api and lets you use it in your app as React components.
Here are the main additions to react-google-maps that were the motivation behind this re-write
with NPM
npm i -S @react-google-maps/api
or Yarn
yarn add @react-google-maps/api
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() {
const [map, setMap] = React.useState(null)
const onLoad = React.useCallback(function callback(map) {
const bounds = new window.google.maps.LatLngBounds();
map.fitBounds(bounds);
setMap(map)
}, [])
const onUnmount = React.useCallback(function callback(map) {
setMap(null)
}, [])
return (
<LoadScript
googleMapsApiKey="YOUR_API_KEY"
>
<GoogleMap
mapContainerStyle={containerStyle}
center={center}
zoom={10}
onLoad={onLoad}
onUnmount={onUnmount}
>
{ /* Child components, such as markers, info windows, etc. */ }
<></>
</GoogleMap>
</LoadScript>
)
}
export default React.memo(MyComponent)
if you need an access to map object, instead of ref
prop, you need to use onLoad
callback on <GoogleMap />
component.
Before:
// before - don't do that!
<GoogleMap
ref={map => {
const bounds = new window.google.maps.LatLngBounds();
map.fitBounds(bounds);
}}
/>
After:
<GoogleMap
onLoad={map => {
const bounds = new window.google.maps.LatLngBounds();
map.fitBounds(bounds);
}}
onUnmount={map => {
// do your stuff before map is unmounted
}}
/>
If you want to use window.google
object, you need to extract GoogleMap in separate module, so it is lazy executed then google-maps-api
script is loaded and executed by <LoadScript />
. If you try to use window.google
before it is loaded it will be undefined and you'll get a TypeError.
12.4kb
gzip, tree-shakeable https://bundlephobia.com/result?p=@react-google-maps/api<LoadScript preventGoogleFonts />
componentExamples can be found in two places:
Using the examples requires you to generate a google maps api key. For instructions on how to do that please see the following guide
You can join the community at Spectrum.chat to ask questions and help others with your experience or join our Slack channel
Maintainers and contributors are very welcome! See this issue to get started.
When working on a feature/fix, you're probably gonna want to test your changes. This workflow is a work in progress. Please feel free to improve it!
packages/react-google-maps-api/package.json
change main
to "src/index.ts"
module
fieldreact-google-maps-api-gatsby-example
to test your changes. Just make sure you change the import from @react-google-maps/api
to ../../../react-google-maps-api
Since 1.2.0 you can use onLoad and onMount props for each @react-google-maps/api component, ref does not contain API methods anymore.
Since version 1.2.2 We added useGoogleMap hook, which is working only with React@16.8.1 and later versions.
DriveFromTo.com Transfer Booking service PWA.
Shipwrecks.cc Shipwrecks from Wikipedia visualized on the map (Github)
nycmesh.net Network topography visualized on the map (Github)
add your website by making PR!
FAQs
React.js Google Maps API integration
The npm package @react-google-maps/api receives a total of 255,817 weekly downloads. As such, @react-google-maps/api popularity was classified as popular.
We found that @react-google-maps/api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.