Last active
November 18, 2017 03:53
-
-
Save dakala/7c5e967d12840c3ea5d7d26cc8c41c6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { compose, withProps, withHandlers } from "recompose"; | |
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"; | |
import { MarkerClusterer } from "react-google-maps/lib/components/addons/MarkerClusterer"; | |
import axios from 'axios'; | |
const MapWithAMarkerClusterer = compose( | |
withProps({ | |
googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyC4R6AN7SmujjPUIGKdyao2Kqitzr1kiRg&v=3.exp&libraries=geometry,drawing,places", | |
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 DemoMap extends React.PureComponent { | |
componentWillMount() { | |
this.setState({ markers: [] }) | |
} | |
componentDidMount() { | |
const url = [ | |
// Length issue | |
`https://gist.githubusercontent.com`, | |
`/farrrr/dfda7dd7fccfec5474d3`, | |
`/raw/758852bbc1979f6c4522ab4e92d1c92cba8fb0dc/data.json` | |
].join("") | |
axios.get(url) | |
.then(response => { | |
this.setState({ markers: response.data.photos }); | |
}) | |
.catch(error => { | |
console.log(error); | |
}); | |
} | |
render() { | |
return ( | |
<MapWithAMarkerClusterer markers={this.state.markers} /> | |
) | |
} | |
} | |
export default DemoMap; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment