1-
21<p align =" center " >
32 <img src =" resources/readme/fullstackreact-google-maps-tutorial.png " alt =" Fullstack React Google Maps Tutorial " />
43</p >
@@ -49,6 +48,7 @@ If you want to add a loading container _other than the default_ loading containe
4948const LoadingContainer = (props ) => (
5049 < div> Fancy loading container! < / div>
5150)
51+
5252export default GoogleApiWrapper ({
5353 apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE ),
5454 LoadingContainer: LoadingContainer
@@ -61,7 +61,7 @@ export default GoogleApiWrapper({
6161import {Map , InfoWindow , Marker , GoogleApiWrapper } from ' google-maps-react' ;
6262
6363export class MapContainer extends Component {
64- render () {
64+ render () {
6565 return (
6666 < Map google= {this .props .google } zoom= {14 }>
6767
@@ -138,58 +138,55 @@ The `<Map />` component handles events out of the box. All event handlers are op
138138When the ` <Map /> ` instance has been loaded and is ready on the page, it will call the ` onReady ` prop, if given. The ` onReady ` prop is useful for fetching places or using the autocomplete API for places.
139139
140140``` javascript
141- React .createClass ({
142- fetchPlaces : function (mapProps , map ) {
143- const {google } = mapProps;
144- const service = new google.maps.places.PlacesService (map);
145- // ...
146- },
147- render : function () {
148- return (
149- < Map google= {this .props .google }
150- onReady= {this .fetchPlaces }
151- visible= {false }>
152- < Listing places= {this .state .places } / >
153- < / Map >
154- )
155- }
156- });
141+ fetchPlaces (mapProps , map ) {
142+ const {google } = mapProps;
143+ const service = new google.maps.places.PlacesService (map);
144+ // ...
145+ }
146+
147+ render () {
148+ return (
149+ < Map google= {this .props .google }
150+ onReady= {this .fetchPlaces }
151+ visible= {false }>
152+ < Listing places= {this .state .places } / >
153+ < / Map >
154+ )
155+ }
157156```
158157
159158#### onClick
160159
161160To listen for clicks on the ` <Map /> ` component, pass the ` onClick ` prop:
162161
163162``` javascript
164- React .createClass ({
165- mapClicked : function (mapProps , map , clickEvent ) {
166- // ...
167- },
168- render : function () {
169- return (
170- < Map google= {this .props .google }
171- onClick= {this .mapClicked } / >
172- )
173- }
174- });
163+ mapClicked (mapProps , map , clickEvent ) {
164+ // ...
165+ }
166+
167+ render () {
168+ return (
169+ < Map google= {this .props .google }
170+ onClick= {this .mapClicked } / >
171+ )
172+ }
175173```
176174
177175#### onDragend
178176
179177When our user changes the map center by dragging the Map around, we can get a callback after the event is fired with the ` onDragend ` prop:
180178
181179``` javascript
182- React .createClass ({
183- centerMoved : function (mapProps , map ) {
184- // ...
185- },
186- render : function () {
187- return (
188- < Map google= {this .props .google }
189- onDragend= {this .centerMoved } / >
190- )
191- }
192- });
180+ centerMoved (mapProps , map ) {
181+ // ...
182+ }
183+
184+ render () {
185+ return (
186+ < Map google= {this .props .google }
187+ onDragend= {this .centerMoved } / >
188+ )
189+ }
193190```
194191
195192### Visibility
@@ -256,51 +253,52 @@ The `<Marker />` component listens for events, similar to the `<Map />` componen
256253You can listen for an ` onClick ` event with the (appropriately named) ` onClick ` prop.
257254
258255``` javascript
259- const WithMarkers = React . createClass ( {
260- onMarkerClick : function ( props , marker , e ) {
261- },
262- render : function () [
263- return (
264- <Map google={ this . props . google } >
265- < Marker onClick = {this .onMarkerClick }
266- name = { ' Current location ' } / >
267- < / Map >
268- )
269- ]
270- });
256+ onMarkerClick ( props , marker , e ) {
257+ // ..
258+ }
259+
260+ render () {
261+ return (
262+ < Map google = {this .props . google } >
263+ < Marker onClick = { this . onMarkerClick }
264+ name = { ' Current location ' } / >
265+ < / Map >
266+ )
267+ }
271268```
272269
273270#### mouseover
274271
275272You can also pass a callback when the user mouses over a ` <Marker /> ` instance by passing the ` onMouseover ` callback:
276273
277274``` javascript
278- const Container = React . createClass ( {
279- onMouseoverMarker : function ( props , marker , e ) {
280- },
281- render : function () [
282- return (
283- <Map google={ this . props . google } >
284- < Marker onMouseover = {this .onMouseoverMarker }
285- name = { ' Current location ' } / >
286- < / Map >
287- )
288- ]
289- });
275+ onMouseoverMarker ( props , marker , e ) {
276+ // ..
277+ }
278+
279+ render () {
280+ return (
281+ < Map google = {this .props . google } >
282+ < Marker onMouseover = { this . onMouseoverMarker }
283+ name = { ' Current location ' } / >
284+ < / Map >
285+ )
286+ }
290287```
291288
292289### Polygon
293290
294291To place a polygon on the Map, set ` <Polygon /> ` as child of Map component.
295292
296293``` javascript
297- render : function () {
298- var triangleCoords = [
294+ render () {
295+ const triangleCoords = [
299296 {lat: 25.774 , lng: - 80.190 },
300297 {lat: 18.466 , lng: - 66.118 },
301298 {lat: 32.321 , lng: - 64.757 },
302299 {lat: 25.774 , lng: - 80.190 }
303300 ];
301+
304302 return (
305303 < Map google= {this .props .google }
306304 style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -327,13 +325,14 @@ The `<Polygon />` component listens to `onClick`, `onMouseover` and `onMouseout`
327325To place a polyline on the Map, set ` <Polyline /> ` as child of Map component.
328326
329327``` javascript
330- render : function () {
331- var triangleCoords = [
328+ render () {
329+ const triangleCoords = [
332330 {lat: 25.774 , lng: - 80.190 },
333331 {lat: 18.466 , lng: - 66.118 },
334332 {lat: 32.321 , lng: - 64.757 },
335333 {lat: 25.774 , lng: - 80.190 }
336334 ];
335+
337336 return (
338337 < Map google= {this .props .google }
339338 style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -367,37 +366,29 @@ You can use a `position` prop or connect the `<InfoWindow />` component directly
367366``` javascript
368367// note: code formatted for ES6 here
369368export class MapContainer extends Component {
370- constructor (props ) {
371- super (props);
372- this .state = {
373- showingInfoWindow: false ,
374- activeMarker: {},
375- selectedPlace: {},
376- }
369+ state = {
370+ showingInfoWindow: false ,
371+ activeMarker: {},
372+ selectedPlace: {},
373+ };
377374
378- // binding this to event-handler functions
379- this .onMarkerClick = this .onMarkerClick .bind (this );
380- this .onMapClicked = this .onMapClicked .bind (this );
381- }
382-
383- onMarkerClick : function (props , marker , e ) {
375+ onMarkerClick = (props , marker , e ) =>
384376 this .setState ({
385377 selectedPlace: props,
386378 activeMarker: marker,
387379 showingInfoWindow: true
388380 });
389- },
390381
391- onMapClicked : function (props ) {
382+ onMapClicked = (props ) => {
392383 if (this .state .showingInfoWindow ) {
393384 this .setState ({
394385 showingInfoWindow: false ,
395386 activeMarker: null
396387 })
397388 }
398- },
389+ };
399390
400- render : function () {
391+ render () {
401392 return (
402393 < Map google= {this .props .google }
403394 onClick= {this .onMapClicked }>
@@ -414,7 +405,7 @@ export class MapContainer extends Component {
414405 < / Map >
415406 )
416407 }
417- });
408+ }
418409```
419410
420411### Events
0 commit comments