UNPKG

9.42 kBMarkdownView Raw
1## GoogleMap API
2
3### parameters
4
5#### apiKey (string) (_Deprecated use bootstrapURLKeys_)
6
7Google maps api key.
8
9#### bootstrapURLKeys (object)
10
11Example:
12
13```javascript
14<GoogleMap
15 bootstrapURLKeys={{
16 key: API_KEY,
17 language: 'ru',
18 region: 'ru',
19 ...otherUrlParams,
20 }}
21>
22```
23
24#### defaultCenter (array or object)
25
26`[lat, lng]` or `{ lat: lat, lng: lng}`
27Default lat/lng at which to center the map - changing this prop throws a warning
28
29#### center (array or object)
30
31`[lat, lng]` or `{ lat: lat, lng: lng}`
32Lat/lng at which to center the map
33
34#### defaultZoom: (number)
35
36Default map zoom level - changing this prop throws a warning
37
38#### zoom (number)
39
40Map zoom level
41
42#### hoverDistance (number)
43
44Default: 30
45
46#### margin (array)
47
48In onChange callback, gives you a marginBounds argument property, where lat lng will be shifted using margin you have set. For example, you could use a simple check pointInRect to not show Markers near map bounds.
49
50#### debounced (bool)
51
52Default: true
53
54#### layerTypes (string[])
55
56You can add some "layers" for map like a
57[traffic](https://developers.google.com/maps/documentation/javascript/examples/layer-traffic) or
58[transit](https://developers.google.com/maps/documentation/javascript/examples/layer-transit)
59
60```javascript
61layerTypes={['TrafficLayer', 'TransitLayer']}
62```
63
64### callbacks
65
66#### options (func|object)
67
68Set map options such as controls positions / styles, etc.
69
70Example:
71
72```javascript
73createMapOptions: function (maps) {
74 return {
75 panControl: false,
76 mapTypeControl: false,
77 scrollwheel: false,
78 styles: [{ stylers: [{ 'saturation': -100 }, { 'gamma': 0.8 }, { 'lightness': 4 }, { 'visibility': 'on' }] }]
79 }
80 }
81
82 <GoogleMap options={createMapOptions} ... />
83```
84See "Custom map options example" in Examples below for a further example.
85See full options at [Google Maps Javascript API docs](https://developers.google.com/maps/documentation/javascript/controls#ControlOptions)
86
87#### onClick (func)
88
89```
90({ x, y, lat, lng, event })
91```
92
93The `event` prop in args is the outer div onClick event, not the gmap-api 'click' event.
94
95Example:
96
97 ```javascript
98 _onClick = ({x, y, lat, lng, event}) => console.log(x, y, lat, lng, event)
99 // ES5 users
100 function _onClick(obj){ console.log(obj.x, obj.y, obj.lat, obj.lng, obj.event);}
101
102 <GoogleMap onClick={_onClick} ... />
103 ```
104
105#### onBoundsChange (func) (_Deprecated use onChange_)
106
107```
108({ center, zoom, bounds, marginBounds })
109```
110```
111[lat, lng] = center;
112[topLat, leftLng, bottomLat, rightLng] = bounds;
113```
114
115#### resetBoundsOnResize (bool)
116
117When true this will reset the map bounds if the parent resizes.
118
119Default: false
120
121#### onChildClick (func)
122
123#### onChildMouseEnter (func)
124
125#### onChildMouseLeave (func)
126
127#### onZoomAnimationStart (func)
128
129#### onDrag ((map) => void)
130
131#### onZoomAnimationEnd (func)
132
133#### onMapTypeIdChange (func)
134When the user changes the map type (HYBRID, ROADMAP, SATELLITE, TERRAIN) this fires
135
136#### distanceToMouse (func)
137
138#### googleMapLoader (func)
139
140#### onGoogleApiLoaded (func)
141Directly access the maps API - *use at your own risk!*
142
143#### onTilesLoaded (func)
144This function is called when the visible tiles have finished loading.
145
146```javascript
147<GoogleMap onGoogleApiLoaded={({map, maps}) => console.log(map, maps)} />
148```
149
150To prevent warning message add _yesIWantToUseGoogleMapApiInternals_ property to GoogleMap
151
152```javascript
153<GoogleMap onGoogleApiLoaded={({map, maps}) => console.log(map, maps)}
154 yesIWantToUseGoogleMapApiInternals
155 />
156 ```
157
158#### overlayViewDivStyle (object)
159
160Add custom style to `div` (marker container element) created by OverlayView, for example: `{pointerEvents: 'none'}`.
161
162## Child Component API
163
164### parameters
165
166#### lat (number)
167Latitude to place the marker component
168
169#### lng (number)
170Longitude to place the marker component
171
172#### $hover (bool) [automatic]
173GoogleMap passes a $hover prop to hovered components. To detect hover it an uses internal mechanism, explained in x_distance_hover example
174
175Example:
176```javascript
177render() {
178 const style = this.props.$hover ? greatPlaceStyleHover : greatPlaceStyle;
179
180 return (
181 <div style={style}>
182 {this.props.text}
183 </div>
184 );
185 }
186 ```
187
188
189## Utility functions
190
191#### fitBounds (func)
192 Use fitBounds to get zoom and center.
193
194Example:
195
196```javascript
197import { fitBounds } from 'google-map-react/utils';
198
199const bounds = {
200 nw: {
201 lat: 50.01038826014866,
202 lng: -118.6525866875
203 },
204 se: {
205 lat: 32.698335045970396,
206 lng: -92.0217273125
207 }
208};
209
210// Or
211
212const bounds = {
213 ne: {
214 lat: 50.01038826014866,
215 lng: -118.6525866875
216 },
217 sw: {
218 lat: 32.698335045970396,
219 lng: -92.0217273125
220 }
221};
222
223const size = {
224 width: 640, // Map width in pixels
225 height: 380, // Map height in pixels
226};
227
228const {center, zoom} = fitBounds(bounds, size);
229```
230
231#### tile2LatLng (func)
232
233#### latLng2Tile (func)
234
235#### getTilesIds (func)
236
237## Tips
238
239### My map doesn't appear
240
241Make sure the container element has width and height. The map will try to fill the parent container, but if the container has no size, the map will collapse to 0 width / height.
242
243### Positioning a marker
244
245Initially any map object has its top left corner at lat lng coordinates. It's up to you to set the object origin to 0,0 coordinates.
246
247Example (centering the marker):
248
249```javascript
250const greatPlaceStyle = {
251 position: 'absolute',
252 transform: 'translate(-50%, -50%)';
253}
254```
255
256```javascript
257render() {
258 return (
259 <div style={greatPlaceStyle}>
260 {this.props.text}
261 </div>
262 );
263}
264```
265
266### Rendering in a modal
267
268If at the moment of GoogleMap control created, a modal has no size (width,height=0) or/and not displayed, the simple solution is to add something like this in render:
269
270```javascript
271render() {
272 return this.props.modalIsOpen
273 ? <GoogleMap />
274 : null;
275}
276```
277
278### Adding a SearchBox
279
280```javascript
281import React from 'react';
282import ReactDOM from 'react-dom';
283
284export default class SearchBox extends React.Component {
285 static propTypes = {
286 placeholder: React.PropTypes.string,
287 onPlacesChanged: React.PropTypes.func
288 }
289 render() {
290 return <input ref="input" {...this.props} type="text"/>;
291 }
292 onPlacesChanged = () => {
293 if (this.props.onPlacesChanged) {
294 this.props.onPlacesChanged(this.searchBox.getPlaces());
295 }
296 }
297 componentDidMount() {
298 var input = ReactDOM.findDOMNode(this.refs.input);
299 this.searchBox = new google.maps.places.SearchBox(input);
300 this.searchBox.addListener('places_changed', this.onPlacesChanged);
301 }
302 componentWillUnmount() {
303 // https://developers.google.com/maps/documentation/javascript/events#removing
304 google.maps.event.clearInstanceListeners(this.searchBox);
305 }
306}
307```
308
309You will need to preload the google maps API, but `google-map-react` checks if the base api is already loaded,
310and if so, uses it, so it won't load a second copy of the library.
311
312```html
313<script type="text/javascript" src="https://maps.google.com/maps/api/js?libraries=places"></script>
314```
315
316### Override the default minimum zoom
317
318*WARNING*: Setting this option can break markers calculation, causing no homeomorphism between screen coordinates and map.
319
320You can use the `minZoom` custom option to prevent our minimum-zoom calculation:
321
322```javascript
323function createMapOptions() {
324 return {
325 minZoom: 2,
326 };
327}
328```
329
330### Define touch device behavior of scrolling & panning for the map
331
332Google Maps provides control over the behavior of touch based interaction with the map.
333For example, on mobile devices swiping up on the map might mean two things: Scrolling the container or panning the map.
334To resolve this ambigiuity, you can use the custom map option `gestureHandling` to get the required behavior.
335
336```javascript
337function createMapOptions() {
338 return {
339 gestureHandling: 'greedy' // Will capture all touch events on the map towards map panning
340 }
341}
342```
343
344The default setting is `gestureHandling:auto` which tries to detect based on the page/content sizes if a `greedy` setting is best (no scrolling is required) or `cooperative` (scrolling is possible)
345
346For more details see the [google documentation](https://developers.google.com/maps/documentation/javascript/interaction) for this setting.
347
348### Heatmap Layer
349
350For enabling heatmap layer, just add `heatmapLibrary={true}` and provide data for heatmap in `heatmap` as props.
351
352#### Example
353
354```javascript
355<GoogleMapReact
356 bootstrapURLKeys={{ key: [YOUR_KEY] }}
357 zoom={zoom}
358 center={center}
359 heatmapLibrary={true}
360 heatmap={{data}}
361 >
362 {markers}
363 </GoogleMapReact>
364```
365
366#### Important Note
367
368If you have multiple `GoogleMapReact` components in project and you want to use heatmap layer so provide `heatmapLibrary={true}` for all `GoogleMapReact` components so component will load heatmap library at the beginning with google map api.
369
370### Localizing the Map
371
372This is done by setting bootstrapURLKeys.[language](https://developers.google.com/maps/documentation/javascript/localization#Language) and bootstrapURLKeys.[region](https://developers.google.com/maps/documentation/javascript/localization#Region). Also notice that setting region to 'cn' is required when using the map from within China, see [google documentation](https://developers.google.com/maps/documentation/javascript/localization#GoogleMapsChina) for more info. Setting 'cn' will result in use of the specific API URL for China.