UNPKG

@syncromatics/react-google-maps

Version:
80 lines (72 loc) 1.96 kB
### Map with a MarkerClusterer ```jsx const fetch = require("isomorphic-fetch"); const { compose, withProps, withHandlers } = require("recompose"); const { withScriptjs, withGoogleMap, GoogleMap, Marker, } = require("@syncromatics/react-google-maps"); const { MarkerClusterer } = require("@syncromatics/react-google-maps/lib/components/addons/MarkerClusterer"); const MapWithAMarkerClusterer = compose( withProps({ googleMapURL: GOOGLE_MAP_URL, loadingElement: <div style={{ height: `100%` }} />, containerElement: <div style={{ height: `400px` }} />, mapElement: <div style={{ height: `100%` }} />, }), withHandlers({ onMarkerClustererClick: () => (markerClusterer) => { const clickedMarkers = markerClusterer.getMarkers() console.log(`Current clicked markers length: ${clickedMarkers.length}`) console.log(clickedMarkers) }, }), withScriptjs, withGoogleMap )(props => <GoogleMap defaultZoom={3} defaultCenter={{ lat: 25.0391667, lng: 121.525 }} > <MarkerClusterer onClick={props.onMarkerClustererClick} averageCenter enableRetinaIcons gridSize={60} > {props.markers.map(marker => ( <Marker key={marker.photo_id} position={{ lat: marker.latitude, lng: marker.longitude }} /> ))} </MarkerClusterer> </GoogleMap> ); class DemoApp extends React.PureComponent { componentWillMount() { this.setState({ markers: [] }) } componentDidMount() { const url = [ // Length issue `https://gist.githubusercontent.com`, `/farrrr/dfda7dd7fccfec5474d3`, `/raw/758852bbc1979f6c4522ab4e92d1c92cba8fb0dc/data.json` ].join("") fetch(url) .then(res => res.json()) .then(data => { this.setState({ markers: data.photos }); }); } render() { return ( <MapWithAMarkerClusterer markers={this.state.markers} /> ) } } <DemoApp /> ```