-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtestmap.html
233 lines (198 loc) · 6.81 KB
/
testmap.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
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
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html>
<head>
<title>Smooth Runnings</title>
<script type="application/javascript">
'use strict';
/* identification division.
* program-id. YouCantTouchThis-BreakItDown.
* author. Richard Maher.
* version. 1.0
*/
document.addEventListener("DOMContentLoaded", isDOMIsGood);
const MARKER_SELECTOR = "img[src*='hg.png']";
const MARKER_SRC = "https://richardmaher.github.io/Brotkrumen/hg.png";
const TILE_SIZE = 256;
const MAX_TRIP = 99999;
const TOO_SMALL = 0;
var mapDiv, protagonists, HandG, map, observer, markerDiv,
markerStyle, travelListener;
var dest = [
{ lat: -31.9523, lng: 115.8613 },
{ lat: -33.9525, lng: 150.1932 },
// { lat: 0.5821, lng: 150.1932 }, // z12 failure. 5820 works
// { lat: -33.8688, lng: 150.2093 },
{ lat: -27.4705, lng: 153.0260 },
{ lat: -34.9285, lng: 138.6007 }
];
var destIndex = 0;
var touchDown = false;
var myLatLng = { lat: -25.363, lng: 131.044 };
// var myLatLng = {lat: -31.9523, lng: 115.86096 }; // 99999 pixels from 150.1932 at zoom 12
function isDOMIsGood(e) {
console.log("DOM");
mapDiv = document.getElementById("map");
}
function initMap() {
console.log("Init");
map = new google.maps.Map(mapDiv, {
zoom: 4,
// zoom: 13,
center: myLatLng
});
observer = new MutationObserver(waitForMarker);
observer.observe(mapDiv, {
childList : true,
subtree : true ,
attributes : true ,
characterData : false
});
protagonists = {size: new google.maps.Size(45, 40),
scaledSize: new google.maps.Size(45, 40),
position: myLatLng,
url: MARKER_SRC};
google.maps.event.addListenerOnce(map,'tilesloaded', tilesUp);
}
function tilesUp() {
travelListener = google.maps.event.addListener(map, 'center_changed', centerChanged);
HandG = new google.maps.Marker(
{
position: myLatLng,
map: map,
draggable: false,
title: "HandG",
zIndex: 13,
visible: true,
icon: protagonists,
optimized: false
});
}
function makeDestCenter(){
console.log("Panning to new Center " + map.getZoom());
var home = map.getCenter();
var zoom = map.getZoom();
var scale = 1 << zoom;
var proj = map.getProjection();
var homePoint = proj.fromLatLngToPoint(home);
var startPixelX = Math.round(homePoint.x * scale);
var startPixelY = Math.round(homePoint.y * scale);
var destPoint = proj.fromLatLngToPoint(dest[destIndex]);
var destPixelX = Math.round(destPoint.x * scale);
var destPixelY = Math.round(destPoint.y * scale);
var xTrip = Math.abs(destPixelX - startPixelX);
var yTrip = Math.abs(destPixelY - startPixelY);
console.log("sX " + startPixelX + " dX " + destPixelX + " sY " + startPixelY + " dY " + destPixelY);
if ((xTrip > MAX_TRIP) || (yTrip > MAX_TRIP)) {
google.maps.event.addListenerOnce(map, 'idle', makeDestCenter);
map.setZoom(--zoom);
} else {
if (xTrip == TOO_SMALL && yTrip == TOO_SMALL) {
google.maps.event.addListenerOnce(map, 'idle', makeDestCenter);
map.setZoom(++zoom);
} else {
map.panTo(dest[destIndex]);
}
}
}
function centerChanged() {
console.log("Center Changed ");
google.maps.event.addListenerOnce(map, 'idle', reHome);
}
function reHome() {
console.log("Re Home ");
markerDiv.style.transitionDuration = "1ms";
markerDiv.style.transitionTimingFunction = "linear";
markerDiv.style.transitionProperty = "left, top";
markerDiv.addEventListener('transitionend', quiesced, { 'once': true });
markerDiv.addEventListener('transitioncancel', quiesced, { 'once': true });
markerDiv.style.visibility = "hidden";
}
function quiesced(e) {
markerStyle = getComputedStyle(markerDiv);
markerDiv.style.visibility = "visible";
console.log("Quiesced left " + markerStyle.left + " top " + markerStyle.top);
setTimeout(handover, 0);
}
function handover(e) {
markerStyle = getComputedStyle(markerDiv);
console.log("in handover left " + markerStyle.left + " top " + markerStyle.top);
markerDiv.style.transitionDuration = 2000 + "ms";
markerDiv.style.transitionTimingFunction = "linear";
markerDiv.addEventListener('transitionend', incrSteps, { 'once': true });
markerDiv.style.transitionProperty = "left, top";
markerDiv.style.visibility = "visible";
HandG.setPosition(dest[destIndex]);
myLatLng = dest[destIndex];
}
function startLeg(e) {
console.log("start leg " + e.propertyName + " left " + e.target.style.left + " top " + e.target.style.top);
}
function cancelLeg(e) {
console.log("Cancel leg " + e.propertyName + " left " + e.target.style.left + " top " + e.target.style.top);
markerDiv.style.transitionDuration = "0s";
}
function incrSteps(e) {
console.log("Incr Steps " + e.propertyName + " left " + e.target.style.left + " top " + e.target.style.top);
if (++destIndex >= dest.length) {
console.log("Journey's end");
markerDiv.style.transitionDuration = "0s";
travelListener.remove();
return;
}
setTimeout(makeDestCenter,0);
}
function waitForMarker(mutations, myInstance) {
outer:
for (var i=0; i<mutations.length; i++){
if (mutations[i].type == "attributes" &&
mutations[i].target.tagName == "IMG" &&
mutations[i].target.src.toLowerCase().indexOf(MARKER_SRC.toLowerCase()) != -1){
touchDown = true;
break outer;
}
if (mutations[i].type != "childList" ||
mutations[i].addedNodes.length == 0)
continue;
for (var j=0; j<mutations[i].addedNodes.length; j++) {
var node = mutations[i].addedNodes[j];
if (node.tagName == "DIV" && node.firstChild && node.firstChild.tagName == "IMG" &&
node.firstChild.src.toLowerCase().indexOf(MARKER_SRC.toLowerCase()) != -1){
touchDown = true;
break outer;
}
}
}
if (touchDown) {
console.log("Got Marker");
myInstance.disconnect();
var markerImgs = document.querySelectorAll(MARKER_SELECTOR);
if (markerImgs.length != 1) {
throw new Error("Expecting one and only one Hansel and Gretal. Found: " + markerImgs.length);
};
markerDiv = markerImgs[0].parentNode;
markerDiv.addEventListener('transitionstart', startLeg);
markerDiv.addEventListener('transitioncancel', cancelLeg);
setTimeout(makeDestCenter,0);
}
}
</script>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB41DRUbKWJHPxaFjMAwdrzWzbVKartNGg&callback=initMap&v=weekly&channel=2" async
></script>
<style type="text/css">
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>