Skip to content

Instantly share code, notes, and snippets.

@bingeboy
Created September 9, 2015 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bingeboy/2ab6d546cf1ce8359912 to your computer and use it in GitHub Desktop.
Save bingeboy/2ab6d546cf1ce8359912 to your computer and use it in GitHub Desktop.
es6 google map react component
'use strict';
import React from 'react/addons';
import ReactGoogleMaps, {Map, Marker} from 'react-googlemaps';
const GoogleMapsAPI = window.google.maps;
const LatLng = GoogleMapsAPI.LatLng;
export default class Gmap extends React.Component{
constructor() {
super();
this.state = {
center: new LatLng(-34.397, 150.644),
zoom: 12,
markers: [
{position: new LatLng(-34.397, 150.644)},
{position: new LatLng(-34.500, 150.644)}
]
}
this.handleMapClick = this.handleMapClick.bind(this)
this.handleCenterChange = this.handleCenterChange.bind(this)
}
renderMarkers(markerState, i) {
return <Marker position={markerState.position} key={i} />
}
handleMapClick(mapEvent) {
let marker = {
position: mapEvent.latLng
};
let markers = React.addons
.update(this.state.markers, {$push: [marker]});
this.setState({
markers: markers,
center: mapEvent.latLng
});
}
handleCenterChange(map) {
this.setState({
center: map.getCenter()
});
}
render() {
return <Map
initialZoom={this.state.zoom}
center={this.state.center}
onCenterChange={this.handleCenterChange}
width={700}
height={700}
onClick={this.handleMapClick}>
{this.state.markers.map(this.renderMarkers)}
</Map>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment