forked from achowd53/WaldoStreet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waldo.html
157 lines (128 loc) · 4.64 KB
/
waldo.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
<!DOCTYPE html>
<html>
<body>
<!--Nav Bar-->
<ul>
<li id="title"><a onclick="window.location.reload();">Waldo Street</a></li>
<li id="title"><a onclick=resetPosition()>Reset Position</a></li>
</ul>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<div id="googleMap"></div>
<style type="text/css">
html,
body {
height: 97%;
width: 100%;
margin: auto;
padding: 0;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
background-color: #333;
overflow: hidden;
}
li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
#title {
font-family: "Helvetica";
}
#street-view {
height: 100%;
filter: invert(0);
}
</style>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
let map_center = {lat: 0, lng: 0}; //Location of center of displayed panorama
let where_waldo = {lat: 0, lng: 0}; //Location of Waldo
//Generates 360 street view of random street in NYC and adds interactable Waldo marker in
function myMap() {
//Uses Google Cloud Roads API in order to use random geographical coordinates and find nearest road segment to place user in
const coords = {"lat": Math.random() * (0.1) + 0.7 + 40, "lng": -(Math.random() * (0.1) + 0.9 + 73)};
var road = 'https://roads.googleapis.com/v1/nearestRoads?points=' + coords["lat"] + '%2C' + coords["lng"] + '&key=YOUR_KEY';
var config = {
method: 'get',
url: road,
headers: { }
};
axios(config)
.then(response => {
console.log(JSON.stringify(response.data));
const closest = response.data["snappedPoints"][0]["location"]; // {"lat": blah, "long": blah}
const temp = {lat: closest["latitude"], lng: closest["longitude"]}
map_center = temp;
//Uses Google Maps API in order to generate map of location found with Roads API
const map = new google.maps.Map(document.getElementById("street-view"), {
center: temp,
zoom: 1,
streetViewControl: false,
})
//Uses Roads API to find location to place Waldo Google Maps Marker in
let waldo_coords_lat_shift = Math.random()*.0015+.001;
if (Math.random() >= .5) waldo_coords_lat_shift *= -1;
let waldo_coords_lng_shift = Math.random()*.0015+.001;
if (Math.random() >= .5) waldo_coords_lng_shift *= -1;
const waldo_coords = {"lat": closest["latitude"] + waldo_coords_lat_shift, "lng": closest["longitude"] + waldo_coords_lat_shift};
var waldo_road = 'https://roads.googleapis.com/v1/nearestRoads?points=' + waldo_coords["lat"] + '%2C' + waldo_coords["lng"] + '&key=YOUR_KEY';
var waldo_config = {
method: 'get',
url: waldo_road,
headers: { }
};
axios(waldo_config)
.then(waldo_response => {
const waldo_loc = waldo_response.data["snappedPoints"][0]["location"];
const temp_waldo = {lat: waldo_loc["latitude"], lng: closest["longitude"]};
where_waldo = temp_waldo;
const waldo = new google.maps.Marker({
position: temp_waldo,
map,
icon: "https://www3.lunapic.com/editor/working/163259280793127833?4612985720",
title: "Waldo",
});
console.log("Waldo is over here:",temp_waldo);
//Adds listener to display alert to give point and refresh page, if user close enough to Waldo Marker when it is clicked on
waldo.addListener("click", () => {
let position = (panorama.getPosition()+"").split(", ");
let cur_lat = parseFloat(position[0].substring(1,10));
let cur_lng = parseFloat(position[1].substring(0,9));
if (-.00025 <= cur_lat - where_waldo.lat && cur_lat - where_waldo.lat <= .00025 && -.00025 <= cur_lng - where_waldo.lng && cur_lng - where_waldo.lng <= .00025 && !alert("Congratulations, You found a Waldo!")) window.location.reload();
});
})
//Uses Google Maps API to generate panorama/360 Street View of user location created with Roads API
panorama = map.getStreetView();
panorama.setPosition(temp);
panorama.setPov(
{
heading: 165,
pitch: 0,
}
);
panorama.setVisible(true);
})
.catch(error => {
console.log(error);
});
}
//Resets user to initial panorama center position
function resetPosition() {
panorama.setPosition(map_center);
}
</script>
<div id="street-view"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&callback=myMap&libraries=&v=weekly"></script>
</body>
</html>