import React from 'react'; import { compose, withProps, lifecycle } from 'recompose'; import { withScriptjs, withGoogleMap, GoogleMap, Marker, } from 'react-google-maps'; import { SearchBox } from 'react-google-maps/lib/components/places/SearchBox'; const _ = require('lodash'); const MapWithASearchBox = compose( withProps({ googleMapURL: 'https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places', loadingElement:
, containerElement:
, mapElement:
, }), lifecycle({ componentWillMount() { const refs = {}; this.setState({ bounds: null, center: { lat: 41.9, lng: -87.624 }, markers: [], onMapMounted: ref => { refs.map = ref; }, onBoundsChanged: () => { this.setState({ bounds: refs.map.getBounds(), center: refs.map.getCenter(), }); }, onSearchBoxMounted: ref => { refs.searchBox = ref; }, onPlacesChanged: () => { const places = refs.searchBox.getPlaces(); const bounds = new google.maps.LatLngBounds(); // eslint-disable-line places.forEach(place => { if (place.geometry.viewport) { bounds.union(place.geometry.viewport); } else { bounds.extend(place.geometry.location); } }); const nextMarkers = places.map(place => ({ position: place.geometry.location, })); const nextCenter = _.get(nextMarkers, '0.position', this.state.center); this.setState({ center: nextCenter, markers: nextMarkers, }); // refs.map.fitBounds(bounds); }, }); }, }), withScriptjs, withGoogleMap )(props => ( {props.markers.map((marker, index) => )} )); class SearchLocation extends React.Component { render() { return ( ); } } export default SearchLocation;