Skip to content

Commit 862a6e8

Browse files
committed
Allow add any keys to google bootstrap url
1 parent 13277fc commit 862a6e8

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/google_map.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export default class GoogleMap extends Component {
7070

7171
static propTypes = {
7272
apiKey: PropTypes.string,
73+
bootstrapURLKeys: PropTypes.any,
74+
7375
defaultCenter: React.PropTypes.oneOfType([
7476
PropTypes.array,
7577
PropTypes.shape({
@@ -152,6 +154,12 @@ export default class GoogleMap extends Component {
152154
this.childMouseUpTime_ = 0;
153155

154156
if (process.env.NODE_ENV !== 'production') {
157+
if (this.props.apiKey) {
158+
console.warn( 'GoogleMap: ' + // eslint-disable-line no-console
159+
'apiKey is deprecated, use ' +
160+
'bootstrapURLKeys={{key: YOUR_API_KEY}} instead.');
161+
}
162+
155163
if (this.props.onBoundsChange) {
156164
console.warn( 'GoogleMap: ' + // eslint-disable-line no-console
157165
'onBoundsChange is deprecated, use ' +
@@ -193,7 +201,12 @@ export default class GoogleMap extends Component {
193201

194202
window.addEventListener('mouseup', this._onChildMouseUp, false);
195203

196-
this.props.googleMapLoader(this.props.apiKey); // we can start load immediatly
204+
const bootstrapURLKeys = {
205+
...(this.props.apiKey && {key: this.props.apiKey}),
206+
...this.props.bootstrapURLKeys,
207+
};
208+
209+
this.props.googleMapLoader(bootstrapURLKeys); // we can start load immediatly
197210

198211
setTimeout(() => { // to detect size
199212
this._setViewSize();
@@ -307,7 +320,12 @@ export default class GoogleMap extends Component {
307320

308321
this._onBoundsChanged(); // now we can calculate map bounds center etc...
309322

310-
this.props.googleMapLoader(this.props.apiKey)
323+
const bootstrapURLKeys = {
324+
...(this.props.apiKey && {key: this.props.apiKey}),
325+
...this.props.bootstrapURLKeys,
326+
};
327+
328+
this.props.googleMapLoader(bootstrapURLKeys)
311329
.then(maps => {
312330
if (!this.mounted_) {
313331
return;

src/utils/loaders/google_map_loader.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import find from 'lodash/collection/find';
2+
import reduce from 'lodash/collection/reduce';
3+
14
let $script_ = null;
25

36
let _loadPromise;
47

58
// TODO add libraries language and other map options
6-
export default function googleMapLoader(apiKey) {
9+
export default function googleMapLoader(bootstrapURLKeys) {
710
if (!$script_) {
811
$script_ = require('scriptjs');
912
}
@@ -32,15 +35,27 @@ export default function googleMapLoader(apiKey) {
3235
resolve(window.google.maps);
3336
};
3437

35-
const apiKeyString = apiKey ? `&key=${apiKey}` : '';
38+
if (process.env.NODE_ENV !== 'production') {
39+
if (find(Object.keys(bootstrapURLKeys), 'callback')) {
40+
console.error('"callback" key in bootstrapURLKeys is not allowed, ' +
41+
'use onGoogleApiLoaded property instead');
42+
throw new Error('"callback" key in bootstrapURLKeys is not allowed, ' +
43+
'use onGoogleApiLoaded property instead');
44+
}
45+
}
46+
47+
const queryString = reduce(
48+
Object.keys(bootstrapURLKeys),
49+
(r, key) => r + `&${key}=${bootstrapURLKeys[key]}`,
50+
''
51+
);
3652

3753
$script_(
38-
`https://maps.googleapis.com/maps/api/js?callback=_$_google_map_initialize_$_${apiKeyString}`,
39-
() => {
40-
if (typeof window.google === 'undefined') {
41-
reject(new Error('google map initialization error (not loaded)'));
42-
}
43-
});
54+
`https://maps.googleapis.com/maps/api/js?callback=_$_google_map_initialize_$_${queryString}`,
55+
() =>
56+
typeof window.google === 'undefined' &&
57+
reject(new Error('google map initialization error (not loaded)'))
58+
);
4459
});
4560

4661
return _loadPromise;

0 commit comments

Comments
 (0)