forked from 3liz/lizmap-javascript-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GoogleStreetView_3.7.js
222 lines (191 loc) · 7.18 KB
/
GoogleStreetView_3.7.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
lizMap.events.on({
'uicreated': function(e) {
// declare here your google maps key compatible with google street view
var gkey = '';
if ( typeof(google) == 'undefined' ) {
$('body').append('<script async defer src="https://maps.googleapis.com/maps/api/js?key=' + gkey + '&callback=initGoogleStreetView">');
} else {
window.setTimeout(initGoogleStreetView, 1000);
}
}
});
function initGoogleStreetView() {
var gsvMessageTimeoutId;
var cleanGsvMessage = function() {
var gsvMessage = $('#gsv-message');
if ( gsvMessage.length != 0 ) {
gsvMessage.remove();
}
gsvMessageTimeoutId = null;
};
var addGsvMessage = function (aMessage){
if ( gsvMessageTimeoutId ) {
window.clearTimeout(gsvMessageTimeoutId);
}
cleanGsvMessage()
lizMap.addMessage(aMessage, 'info', true).attr('id','gsv-message');
gsvMessageTimeoutId = window.setTimeout(cleanGsvMessage, 5000);
};
var map = lizMap.map;
// get Google Street View layer
var layer = map.getLayersByName('google-street-view');
if ( layer.length == 0 ) {
layer = new OpenLayers.Layer.Vector('google-street-view',{
styleMap: new OpenLayers.StyleMap({
graphicName: 'triangle',
pointRadius: 6,
fill: true,
fillColor: 'yellow',
fillOpacity: 0.4,
stroke: true,
strokeWidth: 3,
strokeColor: 'yellow',
strokeOpacity: 0.8,
rotation: "${angle}"
})
});
map.addLayer(layer);
} else {
return;
}
layer.setVisibility(false);
var drawCtrl = new OpenLayers.Control.DrawFeature(layer,
OpenLayers.Handler.Point,{
eventListeners: {
activate: function(evt) {
layer.destroyFeatures();
layer.setVisibility(true);
},
deactivate: function(evt) {
}
}
}
);
var dragCtrl = new OpenLayers.Control.DragFeature(layer,{
geometryTypes: ['OpenLayers.Geometry.Point'],
type:OpenLayers.Control.TYPE_TOOL,
layout: null,
eventListeners: {
activate: function(evt) {
if (this.layout == null)
return false;
layer.setVisibility(true);
},
deactivate: function(evt) {
layer.setVisibility(false);
layer.destroyFeatures();
}
},
onComplete: function(feature, pixel) {
layer.events.triggerEvent("featuremodified",
{feature: feature});
}
});
map.addControls([drawCtrl, dragCtrl]);
layer.events.on({
featureadded: function(evt) {
// deactivate draw
drawCtrl.deactivate();
// get feature
var feat = layer.features[0];
// clone geometry
var geom = feat.geometry.clone();
geom.transform( layer.projection,'EPSG:4326' );
// Set Panorama
panorama.setPosition({lat: geom.y, lng: geom.x});
panorama.setVisible(true);
// Update feature
feat.attributes.angle = panorama.getPov().heading;
layer.redraw();
// activate drag
dragCtrl.activate();
// update message
addGsvMessage( 'Vous pouvez déplacez la position de votre vue sur la carte.');
},
featuremodified: function(evt) {
// get feature
var feat = evt.feature;
// clone geometry
var geom = feat.geometry.clone();
geom.transform( layer.projection,'EPSG:4326' );
// Set Panorama
panorama.setPosition({lat: geom.y, lng: geom.x});
panorama.setVisible(true);
// Update feature
feat.attributes.angle = panorama.getPov().heading;
layer.redraw();
}
});
var html = '<div id="gsv-pano"></div>';
lizMap.addDock(
'gsv',
'Street view',
'minidock',
html,
'icon-road'
);
var panorama = new google.maps.StreetViewPanorama(
document.getElementById('gsv-pano'), {
position: {lat: 37.869, lng: -122.255},
visible: false
});
panorama.addListener('position_changed', function() {
if ( layer.features.length == 0 )
return;
var pos = panorama.getPosition();
var pt = new OpenLayers.Geometry.Point( pos.lng(), pos.lat() );
pt.transform( 'EPSG:4326', layer.projection );
var feat = layer.features[0];
feat.geometry.x = pt.x;
feat.geometry.y = pt.y;
layer.redraw();
if ( !map.getExtent().scale(0.95).contains(pt.x, pt.y) ){
map.setCenter([pt.x, pt.y]);
}
return;
});
panorama.addListener('pov_changed', function() {
if ( layer.features.length == 0 )
return;
layer.features[0].attributes.angle = panorama.getPov().heading;
layer.redraw();
return;
});
lizMap.events.on({
minidockopened: function(e) {
if ( e.id == 'gsv' ) {
// gsv is displayed in an absolute position, and set its width/height
// regarding the width/height of its host (gsv-pano). So we must
// set width/height of gsv-pano manually. These values depends
// of the width/height of screen.
var headerStyles = window.getComputedStyle(document.getElementById('header'))
var height, width;
if (headerStyles.display == 'none') {
// we are in the iframe mode. No header displayed
var mapStyles = window.getComputedStyle(document.getElementById('map'));
height = (parseFloat(mapStyles.height) * 45 / 100) - 15;
width = document.getElementById('mini-dock').getBoundingClientRect().width - 20 ;
}
else {
// we are in the normal mode
var sidemenuStyles = window.getComputedStyle(document.getElementById('mapmenu'))
var minidockStyles = window.getComputedStyle(document.getElementById('mini-dock'));
height = (parseFloat(sidemenuStyles.height) * 45 / 100) - 15;
width = ((parseFloat(headerStyles.width)-parseFloat(sidemenuStyles.width)) * (parseFloat(minidockStyles.maxWidth)-1) / 100) -15;
}
var gsvPano = document.getElementById('gsv-pano');
gsvPano.style.width = width+'px';
gsvPano.style.height = height+'px';
drawCtrl.activate();
addGsvMessage( 'Cliquez sur la carte pour positionner votre vue.');
}
},
minidockclosed: function(e) {
if ( e.id == 'gsv' ) {
drawCtrl.deactivate();
dragCtrl.deactivate();
panorama.setVisible(false);
}
}
});
}