-
Notifications
You must be signed in to change notification settings - Fork 818
/
GoogleApi.js
49 lines (39 loc) · 1.13 KB
/
GoogleApi.js
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
export const GoogleApi = function(opts) {
opts = opts || {};
if (!opts.hasOwnProperty('apiKey')) {
throw new Error('You must pass an apiKey to use GoogleApi');
}
const apiKey = opts.apiKey;
const libraries = opts.libraries || ['places'];
const client = opts.client;
const URL = opts.url || 'https://maps.googleapis.com/maps/api/js';
const googleVersion = opts.version || '3.31';
let script = null;
let google = (typeof window !== 'undefined' && window.google) || null;
let loading = false;
let channel = null;
let language = opts.language;
let region = opts.region || null;
let onLoadEvents = [];
const url = () => {
let url = URL;
let params = {
key: apiKey,
callback: 'CALLBACK_NAME',
libraries: libraries.join(','),
client: client,
v: googleVersion,
channel: channel,
language: language,
region: region,
onerror: 'ERROR_FUNCTION'
};
let paramStr = Object.keys(params)
.filter(k => !!params[k])
.map(k => `${k}=${params[k]}`)
.join('&');
return `${url}?${paramStr}`;
};
return url();
};
export default GoogleApi;