1
-
2
1
<p align =" center " >
3
2
<img src =" resources/readme/fullstackreact-google-maps-tutorial.png " alt =" Fullstack React Google Maps Tutorial " />
4
3
</p >
@@ -49,6 +48,7 @@ If you want to add a loading container _other than the default_ loading containe
49
48
const LoadingContainer = (props ) => (
50
49
< div> Fancy loading container! < / div>
51
50
)
51
+
52
52
export default GoogleApiWrapper ({
53
53
apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE ),
54
54
LoadingContainer: LoadingContainer
@@ -61,7 +61,7 @@ export default GoogleApiWrapper({
61
61
import {Map , InfoWindow , Marker , GoogleApiWrapper } from ' google-maps-react' ;
62
62
63
63
export class MapContainer extends Component {
64
- render () {
64
+ render () {
65
65
return (
66
66
< Map google= {this .props .google } zoom= {14 }>
67
67
@@ -138,58 +138,55 @@ The `<Map />` component handles events out of the box. All event handlers are op
138
138
When 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.
139
139
140
140
``` 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
+ }
157
156
```
158
157
159
158
#### onClick
160
159
161
160
To listen for clicks on the ` <Map /> ` component, pass the ` onClick ` prop:
162
161
163
162
``` 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
+ }
175
173
```
176
174
177
175
#### onDragend
178
176
179
177
When 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:
180
178
181
179
``` 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
+ }
193
190
```
194
191
195
192
### Visibility
@@ -256,51 +253,52 @@ The `<Marker />` component listens for events, similar to the `<Map />` componen
256
253
You can listen for an ` onClick ` event with the (appropriately named) ` onClick ` prop.
257
254
258
255
``` 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
+ }
271
268
```
272
269
273
270
#### mouseover
274
271
275
272
You can also pass a callback when the user mouses over a ` <Marker /> ` instance by passing the ` onMouseover ` callback:
276
273
277
274
``` 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
+ }
290
287
```
291
288
292
289
### Polygon
293
290
294
291
To place a polygon on the Map, set ` <Polygon /> ` as child of Map component.
295
292
296
293
``` javascript
297
- render : function () {
298
- var triangleCoords = [
294
+ render () {
295
+ const triangleCoords = [
299
296
{lat: 25.774 , lng: - 80.190 },
300
297
{lat: 18.466 , lng: - 66.118 },
301
298
{lat: 32.321 , lng: - 64.757 },
302
299
{lat: 25.774 , lng: - 80.190 }
303
300
];
301
+
304
302
return (
305
303
< Map google= {this .props .google }
306
304
style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -327,13 +325,14 @@ The `<Polygon />` component listens to `onClick`, `onMouseover` and `onMouseout`
327
325
To place a polyline on the Map, set ` <Polyline /> ` as child of Map component.
328
326
329
327
``` javascript
330
- render : function () {
331
- var triangleCoords = [
328
+ render () {
329
+ const triangleCoords = [
332
330
{lat: 25.774 , lng: - 80.190 },
333
331
{lat: 18.466 , lng: - 66.118 },
334
332
{lat: 32.321 , lng: - 64.757 },
335
333
{lat: 25.774 , lng: - 80.190 }
336
334
];
335
+
337
336
return (
338
337
< Map google= {this .props .google }
339
338
style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -367,37 +366,29 @@ You can use a `position` prop or connect the `<InfoWindow />` component directly
367
366
``` javascript
368
367
// note: code formatted for ES6 here
369
368
export 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
+ };
377
374
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 ) =>
384
376
this .setState ({
385
377
selectedPlace: props,
386
378
activeMarker: marker,
387
379
showingInfoWindow: true
388
380
});
389
- },
390
381
391
- onMapClicked : function (props ) {
382
+ onMapClicked = (props ) => {
392
383
if (this .state .showingInfoWindow ) {
393
384
this .setState ({
394
385
showingInfoWindow: false ,
395
386
activeMarker: null
396
387
})
397
388
}
398
- },
389
+ };
399
390
400
- render : function () {
391
+ render () {
401
392
return (
402
393
< Map google= {this .props .google }
403
394
onClick= {this .onMapClicked }>
@@ -414,7 +405,7 @@ export class MapContainer extends Component {
414
405
< / Map >
415
406
)
416
407
}
417
- });
408
+ }
418
409
```
419
410
420
411
### Events
0 commit comments