npm install react-gmap3

> A declarative Google Map React component using React, lazy-loading dependencies, current-location finder and a test-driven approach by the Fullstack React team.
See the demo and accompanying blog post.
First, install the library:
``shell`
npm install --save google-maps-reactAutomatically Lazy-loading Google API
The library includes a helper to wrap around the Google maps API. The GoogleApiWrapper Higher-Order component accepts a configuration object which must include an apiKey. See lib/GoogleApi.js for all options it accepts.
`javascript
import {GoogleApiWrapper} from 'google-maps-react';
// ...
export class MapContainer extends React.Component {}
export default GoogleApiWrapper({
apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE)
})(MapContainer)
`
`javascript
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';
export class MapContainer extends Component {
render() {
return (
{this.state.selectedPlace.name}
);
}
}
export default GoogleApiWrapper({
apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE)
})(MapContainer)
`
Note: Marker and InfoWindow components are disscussed below.

Zoom: (Shown Above) takes a number with the higher value representing a tighter focus on the map's center.
Style: Takes CSS style object - commonly width and height.
`javascript`
const style = {
width: '100%',
height: '100%'
}
initalCenter: Takes an object containing latitude and longitude coordinates. Sets the maps center upon loading.
`javascript`
google={this.props.google}
style={style}
initialCenter={{
lat: 40.854885,
lng: -88.081807
}}
zoom={15}
onClick={this.onMapClicked}
>
It also takes event handlers described below:
The component handles events out of the box. All event handlers are optional.
#### onReady
When the instance has been loaded and is ready on the page, it will call the onReady prop, if given. The onReady prop is useful for fetching places or using the autocomplete API for places.
`javascript`
React.createClass({
fetchPlaces: function(mapProps, map) {
const {google} = mapProps;
const service = new google.maps.places.PlacesService(map);
// ...
},
render: function() {
return (
onReady={this.fetchPlaces}
visible={false}>
)
}
});
#### onClick
To listen for clicks on the component, pass the onClick prop:
`javascript`
React.createClass({
mapClicked: function(mapProps, map, clickEvent) {
// ...
},
render: function() {
return (
onClick={this.mapClicked} />
)
}
});
#### onDragend
When our user changes the map center by dragging the Map around, we can get a callback after the event is fired with the onDragend prop:
`javascript`
React.createClass({
centerMoved: function(mapProps, map) {
// ...
},
render: function() {
return (
onDragend={this.centerMoved} />
)
}
});
You can control the visibility of the map by using the visible prop. This is useful for situations when you want to use the Google Maps API without a map. The component will load like normal. See the Google places demo
For example:
`javascript`
visible={false}>
The api includes subcomponents intended on being used as children of the Map component. Any child can be used within the Map component and will receive the three props (as children):
* map - the Google instance of the map
* - a reference to the window.google objectmapCenter
* - the google.maps.LatLng() object referring to the center of the map instance
To place a marker on the Map, include it as a child of the component.
`javascripts title will appear as a tooltip.'}
style={{width: '100%', height: '100%', position: 'relative'}}
className={'map'}
zoom={14}>
name={'SOMA'}
position={{lat: 37.778519, lng: -122.405640}} />
position={{lat: 37.759703, lng: -122.428093}} />
position={{lat: 37.762391, lng: -122.439192}}
icon={{
url: "/path/to/custom_icon.png",
anchor: new google.maps.Point(32,32),
scaledSize: new google.maps.Size(64,64)
}} />
``
The component accepts a position prop that defines the location for the position on the map. It can be either a raw object or a google.maps.LatLng() instance.
If no position is passed in the props, the marker will default to the current position of the map, i.e. the mapCenter prop.
You can also pass any other props you want with the . It will be passed back through marker events.
The component listens for events, similar to the component.
#### onClick
You can listen for an onClick event with the (appropriately named) onClick prop.
`javascript`
const WithMarkers = React.createClass({
onMarkerClick: function(props, marker, e) {
},
render: function() [
return (
)
]
});
#### mouseover
You can also pass a callback when the user mouses over a instance by passing the onMouseover callback:
`javascript`
const Container = React.createClass({
onMouseoverMarker: function(props, marker, e) {
},
render: function() [
return (
)
]
});
To place a polygon on the Map, set as child of Map component.
`javascript`
render: function() {
var triangleCoords = [
{lat: 25.774, lng: -80.190},
{lat: 18.466, lng: -66.118},
{lat: 32.321, lng: -64.757},
{lat: 25.774, lng: -80.190}
];
return(
style={{width: '100%', height: '100%', position: 'relative'}}
className={'map'}
zoom={14}>
strokeColor="#0000FF"
strokeOpacity={0.8}
strokeWeight={2}
fillColor="#0000FF"
fillOpacity={0.35} />
)
}
#### Events
The component listens to onClick, onMouseover and onMouseout events.
The component included in this library is gives us the ability to pop up a "more info" window on our Google map.

The visibility of the component is controlled by a visible prop. The visible prop is a boolean (PropTypes.bool) that shows the when true and hides it when false.
There are two ways how to control a position of the component.position
You can use a prop or connect the component directly to an existing component by using a marker prop.
`javascript
//note: code formatted for ES6 here
export class MapContainer extends Component {
constructor(props) {
super(props);
this.state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
}
// binding this to event-handler functions
this.onMarkerClick = this.onMarkerClick.bind(this);
this.onMapClicked = this.onMapClicked.bind(this);
}
onMarkerClick: function(props, marker, e) {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
},
onMapClicked: function(props) {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
},
render: function() {
return (
onClick={this.onMapClicked}>
visible={this.state.showingInfoWindow}>
$3
The
throws events when it's showing/hiding. Every event is optional and can accept a handler to be called when the event is fired.`javascript
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
visible={this.state.showingInfoWindow}>
{this.state.selectedPlace.name}
`#### onClose
The
onClose event is fired when the has been closed. It's useful for changing state in the parent component to keep track of the state of the .#### onOpen
The
onOpen event is fired when the window has been mounted in the Google map instance. It's useful for keeping track of the state of the from within the parent component.
The
GoogleApiWrapper automatically passes the google instance loaded when the component mounts (and will only load it once).Manually loading the Google API
If you prefer not to use the automatic loading option, you can also pass the
window.google instance as a prop to your component.`javascript
`Contributing
`shell
git clone https://github.com/fullstackreact/google-maps-react.git
cd google-maps-react
npm install
make dev
``The Google Map React component library uses React and the Google API to give easy access to the Google Maps library.
___
This Google Map React component library was built alongside the blog post How to Write a Google Maps React Component.
This repo was written and is maintained by the Fullstack React team. In the book we cover many more projects like this. We walk through each line of code, explain why it's there and how it works.
This app is only one of several apps we have in the book. If you're looking to learn React, there's no faster way than by spending a few hours with the Fullstack React book.