-
Notifications
You must be signed in to change notification settings - Fork 23
/
google.maps.StreetViewPlayer.js
195 lines (166 loc) · 4.75 KB
/
google.maps.StreetViewPlayer.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Represents a google maps StreetViewPlayer.
*/
google.maps.StreetViewPlayer = function(config) {
this.config = config;
this.config.movieCanvas.innerHTML = "";
var m_sPanoClient = new google.maps.StreetViewService();
var m_aVertices = [];
var m_aFrames = [];
var m_iSensitivity = 15;
var m_iPlayspeed = 300;
var m_iCurrentFrame = 0;
var m_dDirectionsMap = null;
var m_dDirectionsDisplay = null;
var m_bDoneLoading = true;
var m_sCanvasStyle = [];
for(var i=0;i<3;i++) {
m_sCanvasStyle.push(this.config.movieCanvas.appendChild(document.createElement("div")).style);
}
var m_mMarker = null;
var m_iTotalFrames = 0;
var m_bPaused = true;
var m_iVerticesBack = 0;
var self = this;
function loadingMovie() {
for(var i=0;i<m_sCanvasStyle.length;i++) {
m_sCanvasStyle[i].backgroundImage = "none";
}
self.config.onLoading.call(this);
self.setProgress(0);
}
function getPanoramaDataForVertex(vertex) {
m_sPanoClient.getPanoramaByLocation(vertex, m_iSensitivity, function(panoData,status) {
m_iVerticesBack++;
if(status==="OK") {
vertex.panoData = panoData;
} else {
vertex.panoData = null;
}
if(m_iVerticesBack === m_aVertices.length) {
for(var i=0, length = m_aVertices.length;i<length;i++) {
if(m_aVertices[i].panoData===null) {
m_aVertices.splice(i--,1);
}
}
setupFrames();
}
})
}
function setupFrames() {
for(var i=0,length=m_aVertices.length;i<length;i++) {
m_aFrames.push(new google.maps.StreetViewPlayer.Frame(m_aVertices[i], m_aVertices[Math.min(i+1,m_aVertices.length-1)]))
}
m_iTotalFrames = m_aFrames.length;
m_bDoneLoading = true;
self.config.onPlay.call(this);
}
function getDirections() {
var self = this;
m_mMarker = null;
m_bDoneLoading = false;
loadingMovie.call(self);
(new google.maps.DirectionsService()).route({
origin:this.config.origin,
destination:this.config.destination,
travelMode:this.config.travelMode
}, function(result, status) {
if(status == google.maps.DirectionsStatus.OK) {
m_bPaused = true;
m_aVertices = result.routes[0].overview_path;
m_aFrames = [];
m_iTotalFrames = 0;
m_iCurrentFrame = 0;
for(var i=0,length=m_aVertices.length;i<length;i++) {
getPanoramaDataForVertex(m_aVertices[i]);
}
if(m_dDirectionsMap===null) {
m_dDirectionsMap = new google.maps.Map(self.config.mapCanvas,{
zoom:14,
center : m_aVertices[0],
mapTypeId: google.maps.MapTypeId.ROADMAP
});
m_mMarker = new google.maps.Marker({
map: m_dDirectionsMap,
location:m_aVertices[0],
visible:true
})
}
if(m_dDirectionsDisplay===null) {
m_dDirectionsDisplay = new google.maps.DirectionsRenderer();
m_dDirectionsDisplay.setMap(m_dDirectionsMap);
}
m_dDirectionsDisplay.setDirections(result);
self.setPaused(false);
} else {
alert("Error pulling directions for movie, please try again.");
}
})
}
function drawFrame(frame) {
var data = frame.getDisplayData(frame);
for(var i=0,length=data.length;i<length;i++) {
var img = data[i];
m_sCanvasStyle[i].left = img.left;
m_sCanvasStyle[i].backgroundImage = "url("+img.image+")";
m_sCanvasStyle[i].width = img.width || "512px"
m_sCanvasStyle[i].height = img.height || "512px"
}
for(length=m_sCanvasStyle.length;i<length;i++) {
m_sCanvasStyle[i].width = "0px";
}
m_mMarker.setPosition(frame.getPosition());
}
function framePlayer() {
if(m_bPaused===false) {
if(m_iCurrentFrame >= m_iTotalFrames ) {
self.setProgress(m_iTotalFrames);
} else if(m_bPaused===false && m_iTotalFrames > 0 && m_iCurrentFrame<=m_iTotalFrames && m_aFrames[m_iCurrentFrame].isLoaded() ) {
self.setProgress(m_iCurrentFrame);
m_iCurrentFrame++;
}
setTimeout(framePlayer, m_iPlayspeed);
}
};
this.setSensitivity = function(sensitivity) {
m_iSensitivity = sensitivity;
}
this.getSensitivity = function() {
return m_iSensitivity;
}
this.setPlaySpeed = function(playspeed) {
m_iPlayspeed = playspeed;
}
this.getPlaySpeed = function() {
return m_iPlayspeed;
}
this.getPlayerData = function() {
var aData = [];
for(var i=0;i<m_aFrames.length;i++) {
aData.push(m_aFrames[i].getDisplayData());
}
return {
frames : aData
}
}
this.setProgress = function(newFrame) {
m_iCurrentFrame = newFrame;
if(m_iCurrentFrame >=0 && m_iCurrentFrame < m_aFrames.length) {
drawFrame(m_aFrames[m_iCurrentFrame])
}
self.config.onProgress.call(this, parseInt(100*m_iCurrentFrame/m_iTotalFrames));
}
this.setPaused = function(paused) {
m_bPaused = paused;
if(paused===false) {
framePlayer.call(self);
}
}
this.getPaused = function() {
return m_bPaused;
}
this.getTotalFrames = function() {
return m_iTotalFrames;
}
getDirections.call(this)
}