-
Notifications
You must be signed in to change notification settings - Fork 12
/
StreetViewImages.js
148 lines (123 loc) · 5.47 KB
/
StreetViewImages.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function StreetViewImages(options) {
if (!options || !options.apiKey) {
throw new Error("Must provide an google api key");
}
if (!options || !options.addresses || options.addresses.length < 2) {
throw new Error("Must provide at least two addresses");
}
var m_sPanoClient = new google.maps.StreetViewService();
var m_sDirectionsService = new google.maps.DirectionsService();
var m_sApiKey = options.apiKey;
var m_aAddresses = options.addresses;
var m_fOnPanoLoaded = options.onPanoLoaded || function(){};
var m_fOnComplete = options.onComplete || function(){};
var m_aVertices = [];
var m_iSensitivity = 50;
var m_iSpeed = 1000;
var m_iIncrement = 0.012;
var addressIndex = 0;
doLeg();
function doLeg() {
m_sDirectionsService.route({
origin: m_aAddresses[addressIndex],
destination: m_aAddresses[addressIndex + 1],
travelMode: google.maps.TravelMode.DRIVING
}, function(result, status) {
if (status === google.maps.DirectionsStatus.OK) {
for (var i = 0, length = result.routes[0].legs.length; i < length; i++) {
for (var j = 0, lengthJ = result.routes[0].legs[i].steps.length; j < lengthJ; j++) {
for (var k = 0, lengthK = result.routes[0].legs[i].steps[j].lat_lngs.length; k < lengthK; k++) {
m_aVertices.push(result.routes[0].legs[i].steps[j].lat_lngs[k])
}
}
}
for (i = 1, length = m_aVertices.length; i < length; i++) {
if (distanceTo(m_aVertices[i], m_aVertices[i - 1]) < .002) {
m_aVertices.splice(i--, 1);
length--;
}
}
addressIndex++;
if (addressIndex < m_aAddresses.length - 1) {
doLeg();
} else {
var smoothedLocations = getSmoothedLocations(m_aVertices);
pullPanoImages(smoothedLocations);
}
} else {
throw new Error("Error calculating route " + status);
}
});
}
function getSmoothedLocations(vertices) {
var smoothedLocations = [];
smoothedLocations.push(vertices[0]);
var distanceAlongSegment = 0;
for (var i = 1, length = vertices.length; i < length; i++) {
var from = vertices[i - 1];
var to = vertices[i];
var segmentLength = distanceTo(from, to);
while (distanceAlongSegment + m_iIncrement < segmentLength) {
distanceAlongSegment += m_iIncrement;
smoothedLocations.push(tween(from, to, distanceAlongSegment / segmentLength));
}
// Preparing for the next iteration. The distanceAlongSegment will be negative here,
// but it will go positive during the next iteration before we use it again.
distanceAlongSegment = distanceAlongSegment - segmentLength;
}
return smoothedLocations;
}
function toRadians(deg) {
return deg * (Math.PI / 180)
}
function bearingTo(ls, ll) {
var lat1 = toRadians(ls.lat()),
lat2 = toRadians(ll.lat()),
dLon = toRadians(ll.lng()) - toRadians(ls.lng());
return (Math.atan2(Math.sin(dLon) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon)) * 180 / Math.PI + 360) % 360
}
function distanceTo(ls, ll) {
var dLat = toRadians(ll.lat() - ls.lat());
var dLon = toRadians(ll.lng() - ls.lng());
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRadians(ls.lat())) * Math.cos(toRadians(ll.lat())) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return 6371 * c;
}
function tween(start, end, progressRatio) {
var lat = end.lat() * progressRatio + start.lat() * (1 - progressRatio);
var lon = end.lng() * progressRatio + start.lng() * (1 - progressRatio);
return new google.maps.LatLng(lat, lon);
}
function pullPanoImages(vertices, previousBearing) {
if (vertices.length) {
var vertex = vertices.shift();
m_sPanoClient.getPanoramaByLocation(vertex, m_iSensitivity, function (panoData, status) {
if (status === "OK") {
var bearing = previousBearing;
if (vertices.length) {
bearing = bearingTo(vertex, vertices[0]);
}
m_fOnPanoLoaded({
panoData: panoData,
panoUrl: [
"https://maps.googleapis.com/maps/api/streetview?size=640x640",
"pano=" + panoData.location.pano,
"fov=90",
"heading=" + bearing,
"pitch=0",
"key=" + m_sApiKey
].join("&")
});
setTimeout(function() {
pullPanoImages(vertices, bearing);
}, m_iSpeed);
} else {
m_fOnError(new Error(status));
}
});
} else {
m_fOnComplete();
}
}
}
window.StreetViewImages = StreetViewImages;