-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
67 lines (57 loc) · 1.39 KB
/
index.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var jsonp = require('jsonp')
module.exports = function (opts) {
var loaded = false
, loading
, err
, api
return function (cb) {
if (loaded) return cb(err, api)
if (loading) return loading.push(cb)
loading = []
loading.push(cb)
load(opts, function (_err, _api) {
loaded = true
err = _err
api = _api
for (var i = 0; i < loading.length; i++) {
loading[i](err, api)
}
loading = null
})
}
}
module.exports.load = load
function load (opts, cb) {
if (typeof opts == 'function') {
cb = opts
opts = {}
}
opts.sensor = !!opts.sensor
var url = protocol(opts) + '//maps.googleapis.com/maps/api/js?' + query(opts)
jsonp(url, function (err) {
if (err) return cb(new Error('Failed to load Google maps API'))
cb(null, google.maps)
})
}
function protocol (opts) {
var p = opts.protocol || location.protocol
if (!~p.indexOf(':')) p += ':'
if (!~p.indexOf('http')) p = 'http:'
return p
}
function query (opts) {
return stringify(opts, function (key, val, push) {
if (key == 'protocol') return
if (key == 'libraries') return push(key, val.join(','))
push(key, val)
})
}
function stringify (obj, cb) {
var pairs = []
for (var key in obj) {
cb(key, obj[key], function (key, val) {
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(val))
})
}
return pairs.join('&')
}