UNPKG

react-google-maps

Version:
145 lines (128 loc) 3.3 kB
### Usage ```jsx static import fetch from "isomorphic-fetch"; import { compose, withProps } from "recompose"; import { withScriptjs, withGoogleMap, GoogleMap, Marker, } from "react-google-maps"; import MarkerClusterer from "react-google-maps/lib/components/addons/MarkerClusterer"; const MapWithAMarkerClusterer = compose( withProps({ googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places", loadingElement: <div style={{ height: `100%` }} />, containerElement: <div style={{ height: `400px` }} />, mapElement: <div style={{ height: `100%` }} />, }), withScriptjs, withGoogleMap )(props => <GoogleMap defaultZoom={3} defaultCenter={{ lat: 25.0391667, lng: 121.525 }} > <MarkerClusterer 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 /> ``` ### Map with a MarkerClusterer ```jsx const fetch = require("isomorphic-fetch"); const { compose, withProps } = require("recompose"); const { withScriptjs, withGoogleMap, GoogleMap, Marker, } = require("../../index"); const { MarkerClusterer } = require("./MarkerClusterer"); const MapWithAMarkerClusterer = compose( withProps({ googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places", loadingElement: <div style={{ height: `100%` }} />, containerElement: <div style={{ height: `400px` }} />, mapElement: <div style={{ height: `100%` }} />, }), withScriptjs, withGoogleMap )(props => <GoogleMap defaultZoom={3} defaultCenter={{ lat: 25.0391667, lng: 121.525 }} > <MarkerClusterer 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 /> ```