Repository navigation
Expand file tree
/
Copy pathgoogle_map_loader.js
More file actions
76 lines (61 loc) · 2.09 KB
/
Copy pathgoogle_map_loader.js
File metadata and controls
76 lines (61 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import find from 'lodash/collection/find';
import reduce from 'lodash/collection/reduce';
import { Promise } from 'es6-promise';
let $script_ = null;
let loadPromise_;
let resolveCustomPromise_;
const _customPromise = new Promise(resolve => {
resolveCustomPromise_ = resolve;
});
// TODO add libraries language and other map options
export default function googleMapLoader(bootstrapURLKeys) {
if (!$script_) {
$script_ = require('scriptjs');
}
// call from outside google-map-react
// will be as soon as loadPromise_ resolved
if (!bootstrapURLKeys) {
return _customPromise;
}
if (loadPromise_) {
return loadPromise_;
}
loadPromise_ = new Promise((resolve, reject) => {
if (typeof window === 'undefined') {
reject(new Error('google map cannot be loaded outside browser env'));
return;
}
if (window.google && window.google.maps) {
resolve(window.google.maps);
return;
}
if (typeof window._$_google_map_initialize_$_ !== 'undefined') {
reject(new Error('google map initialization error'));
}
window._$_google_map_initialize_$_ = () => {
delete window._$_google_map_initialize_$_;
resolve(window.google.maps);
};
if (process.env.NODE_ENV !== 'production') {
if (find(Object.keys(bootstrapURLKeys), 'callback')) {
console.error('"callback" key in bootstrapURLKeys is not allowed, ' + // eslint-disable-line
'use onGoogleApiLoaded property instead');
throw new Error('"callback" key in bootstrapURLKeys is not allowed, ' +
'use onGoogleApiLoaded property instead');
}
}
const queryString = reduce(
Object.keys(bootstrapURLKeys),
(r, key) => r + `&${key}=${bootstrapURLKeys[key]}`,
''
);
$script_(
`https://maps.googleapis.com/maps/api/js?callback=_$_google_map_initialize_$_${queryString}`,
() =>
typeof window.google === 'undefined' &&
reject(new Error('google map initialization error (not loaded)'))
);
});
resolveCustomPromise_(loadPromise_);
return loadPromise_;
}