React.js Google Maps integration component
For support or usage questions like “how do I do X with React-Google-Maps” and “my code doesn't work”, please search and ask on StackOverflow with a google-maps tag or use react-google-maps as a keyword first.
We ask you to do this because StackOverflow has a much better job at keeping popular questions visible. Unfortunately good answers get lost and outdated on GitHub.
Some questions take a long time to get an answer. If your question gets closed or you don't get a reply on StackOverflow for longer than a few days, we encourage you to post an issue linking to your question. We will close your issue but this will give people watching the repo an opportunity to see your question and reply to it on StackOverflow if they know the answer.
Please be considerate when doing this as this is not the primary purpose of the issue tracker.
As the author (tomchentw) currently doesn't actively use this module, he's looking for awesome contributors to help and keep the community healthy. Please don't hesitate to contact him directly. See #266 for more information.
Basically just a simple wrapper around Google Maps Javascript API. Also check out the demo app and it's source under src/app folder.
Note: this doc is under development for v6.0.0. Find docs for v5.x and v4.x with the git tags.
-
You need to obtain a Google Maps API Key through Google. Follow these steps: Google Maps API Key Reference
-
The first step in using the library is to include the Google Maps script in your
index.html
so that the library has a reference togoogle.maps
.
Statically retrieving the Google Maps Library
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_GOOGLE_MAPS_API_KEY_GOES_HERE"></script>
<!-- Note you can add scopes along with the specific api version with the following configuration -->
<script src="https://maps.googleapis.com/maps/api/js?v=3.27&libraries=places,geometry&key=YOUR_GOOGLE_MAPS_API_KEY_GOES_HERE"></script>
</head>
<body>
<div id="application"></div>
</body>
</html>
Alternatively, if you're going to be loading the map asynchronously through withScriptjs
and want to set the URL through the component and not through your index.html
file you can include it as a prop on <GoogleMap />
Dynamically retrieving the Google Maps Library
import { GoogleMap, Marker } from "react-google-maps";
const googleMapURL="https://maps.googleapis.com/maps/api/js?v=3.27&libraries=places,geometry&key=YOUR_GOOGLE_MAPS_API_KEY_GOES_HERE"
<GoogleMap
ref={props.onMapLoad}
defaultZoom={3}
defaultCenter={{ lat: -25.363882, lng: 131.044922 }}
// Pass the map reference here as props
googleMapURL={googleMapURL}
onClick={props.onMapClick}
>
{props.markers.map((marker, index) => (
<Marker
{...marker}
onRightClick={() => props.onMarkerRightClick(index)}
/>
))}
</GoogleMap>
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps";
// Wrap all `react-google-maps` components with `withGoogleMap` HOC
// and name it GettingStartedGoogleMap
const GettingStartedGoogleMap = withGoogleMap(props => (
<GoogleMap
ref={props.onMapLoad}
defaultZoom={3}
defaultCenter={{ lat: -25.363882, lng: 131.044922 }}
onClick={props.onMapClick}
>
{props.markers.map((marker, index) => (
<Marker
{...marker}
onRightClick={() => props.onMarkerRightClick(index)}
/>
))}
</GoogleMap>
));
// Then, render it:
render(
<GettingStartedGoogleMap
containerElement={
<div style={{ height: `100%` }} />
}
mapElement={
<div style={{ height: `100%` }} />
}
onMapLoad={_.noop}
onMapClick={_.noop}
markers={markers}
onMarkerRightClick={_.noop}
/>,
document.getElementById('root')
);
<GoogleMap
onClick={_.noop}
onRightClick={_.noop}
onDragStart={_.noop}
/>
<Marker
onClick={_.noop}
onRightClick={_.noop}
onDragStart={_.noop}
/>
<Circle
onClick={_.noop}
onRightClick={_.noop}
onDragStart={_.noop}
/>
<Rectangle
onClick={_.noop}
onRightClick={_.noop}
onDragStart={_.noop}
/>
<Polyline
onClick={_.noop}
onRightClick={_.noop}
onDragStart={_.noop}
/>
<Polygon
onClick={_.noop}
onRightClick={_.noop}
onDragStart={_.noop}
/>
<KmlLayer
onClick={_.noop}
onDefaultViewportChanged={_.noop}
onStatusChanged={_.noop}
/>
<FusionTablesLayer
onClick={_.noop}
/>
<InfoWindow
onCloseClick={_.noop}
onDomReady={_.noop}
onZIndexChanged={_.noop}
/>
<StreetViewPanorama
element={<div>Optional placeholder to render StreetView panorama separate from map</div>}
/>
<DrawingManager
onCircleComplete={_.noop}
onOverlayComplete={_.noop}
/>
<SearchBox
inputPlaceholder="Customized your placeholder"
inputStyle={INPUT_STYLE}
/>
<MarkerClusterer
onClusteringBegin={_.noop}
onMouseOut={_.noop}
/>
<InfoBox
onCloseClick={_.noop}
onDomReady={_.noop}
onZIndexChanged={_.noop}
/>
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps";
import withScriptjs from "react-google-maps/lib/async/withScriptjs";
// Wrap all `react-google-maps` components with `withGoogleMap` HOC
// then wraps it into `withScriptjs` HOC
// It loads Google Maps JavaScript API v3 for you asynchronously.
// Name the component AsyncGettingStartedExampleGoogleMap
const AsyncGettingStartedExampleGoogleMap = withScriptjs(
withGoogleMap(
props => (
<GoogleMap
ref={props.onMapLoad}
defaultZoom={3}
defaultCenter={{ lat: -25.363882, lng: 131.044922 }}
onClick={props.onMapClick}
>
{props.markers.map(marker => (
<Marker
{...marker}
onRightClick={() => props.onMarkerRightClick(marker)}
/>
))}
</GoogleMap>
)
)
);
// Then, render it:
render(
<AsyncGettingStartedExampleGoogleMap
googleMapURL="https://maps.googleapis.com/maps/api/js?v=3.exp"
loadingElement={
<div style={{ height: `100%` }}>
<FaSpinner
style={{
display: `block`,
width: `80px`,
height: `80px`,
margin: `150px auto`,
animation: `fa-spin 2s infinite linear`,
}}
/>
</div>
}
containerElement={
<div style={{ height: `100%` }} />
}
mapElement={
<div style={{ height: `100%` }} />
}
onMapLoad={_.noop}
onMapClick={_.noop}
markers={markers}
onMarkerRightClick={_.noop}
/>,
document.getElementById('root')
);
The changelog is automatically generated via conventional-changelog and can be found in project root as well as npm tarball.