-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.html
181 lines (158 loc) · 5.11 KB
/
2.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps Traffic</title>
<style>
html,
body {
font-family: Roboto, Arial, sans-serif;
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
#UI {
position: absolute;
user-select: none;
top: 10px;
left: 200px;
z-index: 9;
font-size: 11px;
}
.btn.on, .btn:active {
font-weight: 500;
background: #2196f3;
color: #fff;
}
.btn.on:hover {
background: #1976d2;
color: #fff;
}
.btn {
padding: 8px;
background: #fff;
border-radius: 2px;
display: inline-block;
text-align: center;
box-shadow: rgba(0, 0, 0, 0.3) 0px 1px 4px -1px;
min-width: 39px;
cursor: pointer;
}
.btn:hover {
background: #eee;
color: #000;
}
</style>
</head>
<body>
<div id="UI">
<div class="btn on" id="toggleTrafficBtn">Traffic</div>
<div class="btn" id="toggleUIBtn">Hide Map</div>
<div class="btn" id="exportBtn">Export</div>
</div>
<div id="map"></div>
<script>
var ui = new UI();
var map, trafficLayer;
function initMap() {
var gMapOptions = {
zoom: 12,
backgroundColor: 'transparent',
center: { // Gold Coast
lat: -27.938433,
lng: 153.407170
},
mapTypeId: google.maps.MapTypeId.ROADMAP,
}
map = new google.maps.Map(document.getElementById("map"), gMapOptions);
trafficLayer = new google.maps.TrafficLayer({
map: map
});
};
var toggleTrafficBtn = document.getElementById('toggleTrafficBtn');
var toggleUIBtn = document.getElementById('toggleUIBtn');
var exportBtn = document.getElementById('exportBtn');
toggleTrafficBtn.addEventListener('click', ui.toggleTraffic);
toggleUIBtn.addEventListener('click', ui.toggleMap);
exportBtn.addEventListener('click', screenshot);
function screenshot() {
ui.toggleUI();
html2canvas(document.body, {
useCORS: true,
background: undefined, // transparent
onrendered: function (canvas) {
ui.toggleUI();
// var img = canvas.toDataURL('image/png');
// download(img, 'file.png', 'image/png');
canvas.toBlob(function (blob) {
download(blob, 'file.png', 'image/png');
}, 'image/png');
}
});
};
function UI() {
var isUIVisible = true;
var isMapVisible = true;
var isTrafficVisible = true;
var UIelement = document.getElementById('UI');
this.toggleUI = function (e) {
isUIVisible = !isUIVisible;
UIelement.style.visibility = isUIVisible ? "visible " : "hidden";
map.setOptions({
disableDefaultUI: !isUIVisible,
});
}
this.toggleMap = function (e) {
e.target.classList.toggle("on");
isMapVisible = !isMapVisible;
map.setOptions({
// disableDoubleClickZoom: true,
// draggable: false,
// scrollwheel: false,
// panControl: false,
// disableDefaultUI: !isMapVisable,
styles: [{
"stylers": [{
"visibility": (isMapVisible ? "on" : "off"),
}]
}]
});
}
this.toggleTraffic = function (e) {
e.target.classList.toggle("on");
isTrafficVisible = !isTrafficVisible;
var temp = isTrafficVisible ? map : null;
trafficLayer.setMap(temp);
}
}
// FireFox
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
console.warn("FireFox makes grainy images. We recommend a different browser.");
}
// canvasToBlobPollyfill
if (!HTMLCanvasElement.prototype.toBlob) {
console.warn("Using Canvas.ToBlob() Pollyfill");
Object.defineProperty(HTMLCanvasElement.prototype, 'toBlob', {
value: function(callback, type, quality) {
var binStr = atob(this.toDataURL(type, quality).split(',')[1]),
len = binStr.length,
arr = new Uint8Array(len);
for (var i = 0; i < len; i++) {
arr[i] = binStr.charCodeAt(i);
}
callback(new Blob([arr], {
type: type || 'image/png'
}));
}
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyDLGb2-Qs3xFIx2TYQ7yKYLTypgo3TGcoY&callback=initMap&libraries=geometry"></script>
<script async defer src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
<script async defer src="download/download.js"></script>
</body>
</html>