@@ -17,11 +17,18 @@ const db = firebase.firestore();
1717
1818const { Provider, Consumer } = createReactContext ( null )
1919
20+ export const MapConsumer = Consumer
21+
2022export const Marker = props =>
2123 < Consumer > {
2224 map => map && < RenderMarker map = { map } { ...props } />
2325 } </ Consumer >
2426
27+ export const Info = props =>
28+ < Consumer > {
29+ map => map && < RenderInfoWindow map = { map } { ...props } />
30+ } </ Consumer >
31+
2532class RenderMarker extends Component {
2633 componentDidMount ( ) {
2734 this . marker = new google . maps . Marker ( this . props )
@@ -36,30 +43,65 @@ class RenderMarker extends Component {
3643 }
3744}
3845
39- class GoogleMap extends Component {
46+ class RenderInfoWindow extends Component {
47+ node = document . createElement ( 'div' )
48+
4049 componentDidMount ( ) {
41- this . loadMap ( this . props ) ;
50+ this . win = new google . maps . InfoWindow ( {
51+ content : this . node ,
52+ ...this . props ,
53+ } )
54+ this . win . addListener ( 'close' , console . log )
4255 }
4356
4457 componentWillReceiveProps ( props ) {
45- this . loadMap ( props ) ;
58+ if ( props . position !== this . props . position )
59+ this . win . setPosition ( props . position )
4660 }
4761
48- loadMap ( { google, onClick } ) {
62+ componentWillUnmount ( ) {
63+ this . win . close ( )
64+ }
65+
66+ render ( ) {
67+ return ReactDOM . createPortal ( this . props . children , this . node )
68+ }
69+ }
70+
71+ class GoogleMap extends React . PureComponent {
72+ componentDidMount ( ) {
73+ this . setup ( this . props ) ;
74+ }
75+
76+ componentWillReceiveProps ( props ) {
77+ this . setup ( props ) ;
78+ }
79+
80+ setup ( { google, onClick } ) {
4981 if ( ! google ) return ;
5082
83+ const map = this . initMap ( google )
84+
85+ onClick && map . addListener ( 'click' , onClick )
86+ }
87+
88+ initMap ( google ) {
89+ if ( this . map ) return this . map
90+
5191 const maps = google . maps ;
5292 const node = this . refs . map ;
5393
5494 const map = new maps . Map ( node ,
5595 {
5696 center : this . props . defaultCenter ,
5797 zoom : this . props . defaultZoom ,
58- mapTypeId : "roadmap"
98+ mapTypeId : this . props . defaultType || "roadmap"
5999 } ) ;
60100
61- onClick && map . addListener ( 'click' , onClick )
101+ this . map = map
62102 this . setState ( { map} )
103+
104+ return map
63105 }
64106
65107 render ( ) {
0 commit comments