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 >
@@ -38,6 +37,7 @@ If you want to add a loading container _other than the default_ loading containe
38
37
const LoadingContainer = (props ) => (
39
38
< div> Fancy loading container! < / div>
40
39
)
40
+
41
41
export default GoogleApiWrapper ({
42
42
apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE ),
43
43
LoadingContainer: LoadingContainer
@@ -50,7 +50,7 @@ export default GoogleApiWrapper({
50
50
import {Map , InfoWindow , Marker , GoogleApiWrapper } from ' google-maps-react' ;
51
51
52
52
export class MapContainer extends Component {
53
- render () {
53
+ render () {
54
54
return (
55
55
< Map google= {this .props .google } zoom= {14 }>
56
56
@@ -127,58 +127,55 @@ The `<Map />` component handles events out of the box. All event handlers are op
127
127
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.
128
128
129
129
``` javascript
130
- React .createClass ({
131
- fetchPlaces : function (mapProps , map ) {
132
- const {google } = mapProps;
133
- const service = new google.maps.places.PlacesService (map);
134
- // ...
135
- },
136
- render : function () {
137
- return (
138
- < Map google= {this .props .google }
139
- onReady= {this .fetchPlaces }
140
- visible= {false }>
141
- < Listing places= {this .state .places } / >
142
- < / Map >
143
- )
144
- }
145
- });
130
+ fetchPlaces (mapProps , map ) {
131
+ const {google } = mapProps;
132
+ const service = new google.maps.places.PlacesService (map);
133
+ // ...
134
+ }
135
+
136
+ render () {
137
+ return (
138
+ < Map google= {this .props .google }
139
+ onReady= {this .fetchPlaces }
140
+ visible= {false }>
141
+ < Listing places= {this .state .places } / >
142
+ < / Map >
143
+ )
144
+ }
146
145
```
147
146
148
147
#### onClick
149
148
150
149
To listen for clicks on the ` <Map /> ` component, pass the ` onClick ` prop:
151
150
152
151
``` javascript
153
- React .createClass ({
154
- mapClicked : function (mapProps , map , clickEvent ) {
155
- // ...
156
- },
157
- render : function () {
158
- return (
159
- < Map google= {this .props .google }
160
- onClick= {this .mapClicked } / >
161
- )
162
- }
163
- });
152
+ mapClicked (mapProps , map , clickEvent ) {
153
+ // ...
154
+ }
155
+
156
+ render () {
157
+ return (
158
+ < Map google= {this .props .google }
159
+ onClick= {this .mapClicked } / >
160
+ )
161
+ }
164
162
```
165
163
166
164
#### onDragend
167
165
168
166
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:
169
167
170
168
``` javascript
171
- React .createClass ({
172
- centerMoved : function (mapProps , map ) {
173
- // ...
174
- },
175
- render : function () {
176
- return (
177
- < Map google= {this .props .google }
178
- onDragend= {this .centerMoved } / >
179
- )
180
- }
181
- });
169
+ centerMoved (mapProps , map ) {
170
+ // ...
171
+ }
172
+
173
+ render () {
174
+ return (
175
+ < Map google= {this .props .google }
176
+ onDragend= {this .centerMoved } / >
177
+ )
178
+ }
182
179
```
183
180
184
181
### Visibility
@@ -245,51 +242,52 @@ The `<Marker />` component listens for events, similar to the `<Map />` componen
245
242
You can listen for an ` onClick ` event with the (appropriately named) ` onClick ` prop.
246
243
247
244
``` javascript
248
- const WithMarkers = React . createClass ( {
249
- onMarkerClick : function ( props , marker , e ) {
250
- },
251
- render : function () [
252
- return (
253
- <Map google={ this . props . google } >
254
- < Marker onClick = {this .onMarkerClick }
255
- name = { ' Current location ' } / >
256
- < / Map >
257
- )
258
- ]
259
- });
245
+ onMarkerClick ( props , marker , e ) {
246
+ // ..
247
+ }
248
+
249
+ render () {
250
+ return (
251
+ < Map google = {this .props . google } >
252
+ < Marker onClick = { this . onMarkerClick }
253
+ name = { ' Current location ' } / >
254
+ < / Map >
255
+ )
256
+ }
260
257
```
261
258
262
259
#### mouseover
263
260
264
261
You can also pass a callback when the user mouses over a ` <Marker /> ` instance by passing the ` onMouseover ` callback:
265
262
266
263
``` javascript
267
- const Container = React . createClass ( {
268
- onMouseoverMarker : function ( props , marker , e ) {
269
- },
270
- render : function () [
271
- return (
272
- <Map google={ this . props . google } >
273
- < Marker onMouseover = {this .onMouseoverMarker }
274
- name = { ' Current location ' } / >
275
- < / Map >
276
- )
277
- ]
278
- });
264
+ onMouseoverMarker ( props , marker , e ) {
265
+ // ..
266
+ }
267
+
268
+ render () {
269
+ return (
270
+ < Map google = {this .props . google } >
271
+ < Marker onMouseover = { this . onMouseoverMarker }
272
+ name = { ' Current location ' } / >
273
+ < / Map >
274
+ )
275
+ }
279
276
```
280
277
281
278
### Polygon
282
279
283
280
To place a polygon on the Map, set ` <Polygon /> ` as child of Map component.
284
281
285
282
``` javascript
286
- render : function () {
287
- var triangleCoords = [
283
+ render () {
284
+ const triangleCoords = [
288
285
{lat: 25.774 , lng: - 80.190 },
289
286
{lat: 18.466 , lng: - 66.118 },
290
287
{lat: 32.321 , lng: - 64.757 },
291
288
{lat: 25.774 , lng: - 80.190 }
292
289
];
290
+
293
291
return (
294
292
< Map google= {this .props .google }
295
293
style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -316,13 +314,14 @@ The `<Polygon />` component listens to `onClick`, `onMouseover` and `onMouseout`
316
314
To place a polyline on the Map, set ` <Polyline /> ` as child of Map component.
317
315
318
316
``` javascript
319
- render : function () {
320
- var triangleCoords = [
317
+ render () {
318
+ const triangleCoords = [
321
319
{lat: 25.774 , lng: - 80.190 },
322
320
{lat: 18.466 , lng: - 66.118 },
323
321
{lat: 32.321 , lng: - 64.757 },
324
322
{lat: 25.774 , lng: - 80.190 }
325
323
];
324
+
326
325
return (
327
326
< Map google= {this .props .google }
328
327
style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -356,37 +355,29 @@ You can use a `position` prop or connect the `<InfoWindow />` component directly
356
355
``` javascript
357
356
// note: code formatted for ES6 here
358
357
export class MapContainer extends Component {
359
- constructor (props ) {
360
- super (props);
361
- this .state = {
362
- showingInfoWindow: false ,
363
- activeMarker: {},
364
- selectedPlace: {},
365
- }
358
+ state = {
359
+ showingInfoWindow: false ,
360
+ activeMarker: {},
361
+ selectedPlace: {},
362
+ };
366
363
367
- // binding this to event-handler functions
368
- this .onMarkerClick = this .onMarkerClick .bind (this );
369
- this .onMapClicked = this .onMapClicked .bind (this );
370
- }
371
-
372
- onMarkerClick : function (props , marker , e ) {
364
+ onMarkerClick = (props , marker , e ) =>
373
365
this .setState ({
374
366
selectedPlace: props,
375
367
activeMarker: marker,
376
368
showingInfoWindow: true
377
369
});
378
- },
379
370
380
- onMapClicked : function (props ) {
371
+ onMapClicked = (props ) => {
381
372
if (this .state .showingInfoWindow ) {
382
373
this .setState ({
383
374
showingInfoWindow: false ,
384
375
activeMarker: null
385
376
})
386
377
}
387
- },
378
+ };
388
379
389
- render : function () {
380
+ render () {
390
381
return (
391
382
< Map google= {this .props .google }
392
383
onClick= {this .onMapClicked }>
@@ -403,7 +394,7 @@ export class MapContainer extends Component {
403
394
< / Map >
404
395
)
405
396
}
406
- });
397
+ }
407
398
```
408
399
409
400
### Events
0 commit comments