forked from Experience-Monks/google-panorama-by-location
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
55 lines (48 loc) · 1.59 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*globals google*/
var assign = require('object-assign')
module.exports = function panoramaByLocation (location, opt, cb) {
if (!location || !Array.isArray(location)) {
throw new TypeError('must provide location [ lat, lng ]')
}
if (typeof opt === 'function') {
cb = opt
opt = {}
}
opt = assign({
radius: 50
}, opt)
var service = opt.service
if (!service) {
if (typeof google === 'undefined' || !google.maps || !google.maps.LatLng) {
throw new Error('tried to use Google API without "google.maps" in global scope\n'
+ ' try using \'google-panorama-by-location/node.js\' instead')
}
service = new google.maps.StreetViewService()
}
var latLng = new google.maps.LatLng(location[0], location[1])
if (typeof service.getPanorama === 'function') {
// v3.21
opt.location = latLng
service.getPanorama({
location: latLng,
preference: opt.preference,
radius: opt.radius,
source: opt.source
}, handleResponse)
} else if (typeof service.getPanoramaByLocation === 'function') {
// v3.20
service.getPanoramaByLocation(latLng, opt.radius, 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 at: [ ' + location.join(', ') + ' ]'))
}
}
}