-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (145 loc) · 5.29 KB
/
main.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
// SV: https://developers.google.com/maps/documentation/javascript/streetview#StreetViewMapUsage
//TODO - add button to take you back to the starting point
let map;
let panorama;
let marker;
let attempts = 0;
let score=0;
let correct_marker;
let round_count = 1;
// TODO: snapshot to valid location THEN update start_loication.
let roundLocation;
const GLOBE_RADIUS = 3958; // Radius of the Earth in miles
function calculateDistance(latLng1, latLng2){
var rlat1 = latLng1.lat() * (Math.PI/180); // Convert degrees to radians
var rlat2 = latLng2.lat() * (Math.PI/180); // Convert degrees to radians
var difflat = rlat2-rlat1; // Radian difference (latitudes)
var difflon = (latLng2.lng()-latLng1.lng()) * (Math.PI/180); // Radian difference (longitudes)
var d = 2 * GLOBE_RADIUS * Math.asin(Math.sqrt(Math.sin(difflat/2)*Math.sin(difflat/2)+Math.cos(rlat1)*Math.cos(rlat2)*Math.sin(difflon/2)*Math.sin(difflon/2)));
return d;
}
function handleSubmitClick(){
//TODO: get their current map location
distance = calculateDistance(marker.getPosition(), roundLocation.latLng)
if(distance<10){
PrettyDistance = (distance).toFixed(2);
} else{
PrettyDistance = Math.round(distance);
}
$('#Distance').text(PrettyDistance)
console.log('distance', distance)
score += calculateScore(distance)
console.log('score', score)
$('#score').text(score)
$('#next-round').show()
$('#submit').hide()
correct_marker = new google.maps.Marker({
position: roundLocation.latLng,
map: map,
icon: {
url: "http://maps.google.com/mapfiles/ms/icons/green-dot.png"
}
});
var bounds = new google.maps.LatLngBounds();
bounds.extend(roundLocation.latLng);
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
// TODO: generateNewLocation
// generateNewLocation()
}
google.maps.event.trigger(map, "resize");
function generateNewLocation(){
//todoBroadway, New York
var streetViewService = new google.maps.StreetViewService();
var STREETVIEW_MAX_DISTANCE = 1000000;
var latLng = new google.maps.LatLng(
(Math.random()*125) - 50, // 75 to -50 is good range
(Math.random()*360) - 180);
console.log('latLng', latLng.lat(), latLng.lng())
// TODO; make rough polygons for oceans, then retry if inside them.
// TODO
// google.maps.StreetViewSource.OUTDOOR
// TODO: use api https://developers.google.com/maps/documentation/streetview/intro
function panoHandler(streetViewPanoramaData, status) {
if (status === google.maps.StreetViewStatus.OK) {
// ok
console.log('ok', streetViewPanoramaData)
roundLocation = streetViewPanoramaData.location;
panorama.setPosition(roundLocation.latLng)
attempts=0;
} else {
// no street view available in this range, or some error occurred
console.log('bad', status)
attempts++;
if (attempts<5 ){
generateNewLocation();
}
}
}
streetViewService.getPanorama({
location: latLng,
radius: STREETVIEW_MAX_DISTANCE,
source: google.maps.StreetViewSource.OUTDOOR
}, panoHandler);
// streetViewService.getPanoramaByLocation(latLng, STREETVIEW_MAX_DISTANCE, panoHandler);
// https://stackoverflow.com/questions/2675032/how-to-check-if-google-street-view-available-and-display-message/7458392#7458392
}
function calculateScore(distance){
return Math.round(100 * (GLOBE_RADIUS / (distance + 1)))
}
function initialize() {
$('#next-round').click(()=>{
correct_marker.setMap(null);
marker.setPosition({lat: 0, lng: 0});
round_count++;
$('#round').text(round_count);
generateNewLocation();
map.setCenter({lat: 0, lng: 0});
map.setZoom(1);
$('#submit').show();
$('#next-round').hide();
});
$('#reset-pano').click(()=>{
panorama.setPosition(roundLocation.latLng)
});
$('#grow_map').click(()=>{
$('#map').width(400);
$('#map').height(300);
google.maps.event.trigger(map, "resize");
});
$('#shrink_map').click(()=>{
$('#map').height(30);
$('#map').width(20);
google.maps.event.trigger(map, "resize");
});
$('#next-round').hide();
// Makes map and street view
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 0, lng: 0},
zoom: 1
});
marker = new google.maps.Marker({
position: {lat: 0, lng: 0},
map: map,
});
//TODO: add clicking to map so you can add a marker.
map.addListener('click', function(e) {
console.log('click ', e.latLng.lat(), e.latLng.lng())
// map.setZoom(8);
// map.setCenter(marker.getPosition());
marker.setPosition(e.latLng);
});
//# TODO: turnoff streeet name overlays
panorama = new google.maps.StreetViewPanorama(
document.getElementById('pano'), {
addressControl: false,
streetViewControl: true,
position: {lat:0,lng:0},
pov: {
heading: 34,
pitch: 10
}
});
$('#submit').click(handleSubmitClick)
generateNewLocation();
}