1-
21<p align =" center " >
32 <img src =" resources/readme/fullstackreact-google-maps-tutorial.png " alt =" Fullstack React Google Maps Tutorial " />
43</p >
@@ -38,6 +37,7 @@ If you want to add a loading container _other than the default_ loading containe
3837const LoadingContainer = (props ) => (
3938 < div> Fancy loading container! < / div>
4039)
40+
4141export default GoogleApiWrapper ({
4242 apiKey: (YOUR_GOOGLE_API_KEY_GOES_HERE ),
4343 LoadingContainer: LoadingContainer
@@ -50,7 +50,7 @@ export default GoogleApiWrapper({
5050import {Map , InfoWindow , Marker , GoogleApiWrapper } from ' google-maps-react' ;
5151
5252export class MapContainer extends Component {
53- render () {
53+ render () {
5454 return (
5555 < Map google= {this .props .google } zoom= {14 }>
5656
@@ -127,58 +127,55 @@ The `<Map />` component handles events out of the box. All event handlers are op
127127When 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.
128128
129129``` 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+ }
146145```
147146
148147#### onClick
149148
150149To listen for clicks on the ` <Map /> ` component, pass the ` onClick ` prop:
151150
152151``` 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+ }
164162```
165163
166164#### onDragend
167165
168166When 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:
169167
170168``` 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+ }
182179```
183180
184181### Visibility
@@ -245,51 +242,52 @@ The `<Marker />` component listens for events, similar to the `<Map />` componen
245242You can listen for an ` onClick ` event with the (appropriately named) ` onClick ` prop.
246243
247244``` 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+ }
260257```
261258
262259#### mouseover
263260
264261You can also pass a callback when the user mouses over a ` <Marker /> ` instance by passing the ` onMouseover ` callback:
265262
266263``` 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+ }
279276```
280277
281278### Polygon
282279
283280To place a polygon on the Map, set ` <Polygon /> ` as child of Map component.
284281
285282``` javascript
286- render : function () {
287- var triangleCoords = [
283+ render () {
284+ const triangleCoords = [
288285 {lat: 25.774 , lng: - 80.190 },
289286 {lat: 18.466 , lng: - 66.118 },
290287 {lat: 32.321 , lng: - 64.757 },
291288 {lat: 25.774 , lng: - 80.190 }
292289 ];
290+
293291 return (
294292 < Map google= {this .props .google }
295293 style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -316,13 +314,14 @@ The `<Polygon />` component listens to `onClick`, `onMouseover` and `onMouseout`
316314To place a polyline on the Map, set ` <Polyline /> ` as child of Map component.
317315
318316``` javascript
319- render : function () {
320- var triangleCoords = [
317+ render () {
318+ const triangleCoords = [
321319 {lat: 25.774 , lng: - 80.190 },
322320 {lat: 18.466 , lng: - 66.118 },
323321 {lat: 32.321 , lng: - 64.757 },
324322 {lat: 25.774 , lng: - 80.190 }
325323 ];
324+
326325 return (
327326 < Map google= {this .props .google }
328327 style= {{width: ' 100%' , height: ' 100%' , position: ' relative' }}
@@ -356,37 +355,29 @@ You can use a `position` prop or connect the `<InfoWindow />` component directly
356355``` javascript
357356// note: code formatted for ES6 here
358357export 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+ };
366363
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 ) =>
373365 this .setState ({
374366 selectedPlace: props,
375367 activeMarker: marker,
376368 showingInfoWindow: true
377369 });
378- },
379370
380- onMapClicked : function (props ) {
371+ onMapClicked = (props ) => {
381372 if (this .state .showingInfoWindow ) {
382373 this .setState ({
383374 showingInfoWindow: false ,
384375 activeMarker: null
385376 })
386377 }
387- },
378+ };
388379
389- render : function () {
380+ render () {
390381 return (
391382 < Map google= {this .props .google }
392383 onClick= {this .onMapClicked }>
@@ -403,7 +394,7 @@ export class MapContainer extends Component {
403394 < / Map >
404395 )
405396 }
406- });
397+ }
407398```
408399
409400### Events
0 commit comments