forked from Experience-Monks/google-panorama-by-id
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
41 lines (36 loc) · 1.21 KB
/
browser.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
/*globals google*/
module.exports = function panoramaById (id, opt, cb) {
if (!id || typeof id !== 'string') {
throw new TypeError('must provide pano id')
}
if (typeof opt === 'function') {
cb = opt
opt = {}
}
opt = opt || {}
var service = opt.service
if (!service) {
if (typeof google === 'undefined' || !google.maps) {
throw new Error('tried to use Google API without "google.maps" in global scope\n'
+ ' try using \'google-panorama-by-id/node.js\' instead')
}
service = new google.maps.StreetViewService()
}
if (typeof service.getPanorama === 'function') {
service.getPanorama({ pano: id }, handleResponse)
} else if (typeof service.getPanoramaByLocation === 'function') {
service.getPanoramaById(id, handleResponse)
} else {
throw new TypeError('must provide valid service with getPanorama or getPanoramaByLocation')
}
function handleResponse (result, status) {
if (/^ok$/i.test(status)) {
result.id = result.location.pano
result.latitude = result.location.latLng.lat()
result.longitude = result.location.latLng.lng()
cb(null, result)
} else {
cb(new Error('could not find street view by id: ' + id))
}
}
}