2020-05-20 03:19:53 +02:00
|
|
|
(function () {
|
|
|
|
var Core = {
|
|
|
|
MAX_SCORE: 1000,
|
|
|
|
|
|
|
|
realPosition: null,
|
|
|
|
panorama: null,
|
|
|
|
guessMap: null,
|
|
|
|
guessMarker: null,
|
|
|
|
resultMap: null,
|
|
|
|
resultMarkers: { guess: null, real: null },
|
|
|
|
googleLink: null,
|
|
|
|
|
|
|
|
getNewPosition: function () {
|
2020-05-20 15:54:09 +02:00
|
|
|
Core.panorama.setVisible(false);
|
|
|
|
|
|
|
|
document.getElementById('loading').style.visibility = 'visible';
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.responseType = 'json';
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
if (this.readyState == 4 && this.status == 200) {
|
|
|
|
Core.realPosition = this.response.position;
|
|
|
|
|
|
|
|
var sv = new google.maps.StreetViewService();
|
|
|
|
sv.getPanorama({ location: this.response.position, preference: google.maps.StreetViewPreference.NEAREST }, Core.loadPano);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xhr.open('GET', 'getNewPosition.json', true);
|
|
|
|
xhr.send();
|
|
|
|
},
|
|
|
|
|
|
|
|
loadPano: function (data, status) {
|
|
|
|
if (status !== google.maps.StreetViewStatus.OK) {
|
|
|
|
Core.getNewPosition();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-20 15:54:09 +02:00
|
|
|
document.getElementById('loading').style.visibility = 'hidden';
|
|
|
|
|
|
|
|
Core.panorama.setVisible(true);
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.panorama.setPov({ heading: 0, pitch: 0, zoom: 1 });
|
|
|
|
Core.panorama.setPano(data.location.pano);
|
|
|
|
},
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
calculateScore: function (distance) {
|
|
|
|
var goodness = 1.0 - distance / Math.sqrt(mapArea);
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
return Math.pow(Core.MAX_SCORE, goodness);
|
|
|
|
},
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
calculateScoreBarProperties: function (score) {
|
2020-05-20 14:59:48 +02:00
|
|
|
var percent = Math.ceil((score / Core.MAX_SCORE) * 100);
|
2020-05-20 03:19:53 +02:00
|
|
|
|
|
|
|
var color;
|
|
|
|
if (percent >= 90) {
|
|
|
|
color = '#11ca00';
|
|
|
|
} else if (percent >= 10) {
|
|
|
|
color = '#ea9000';
|
|
|
|
} else {
|
|
|
|
color = '#ca1100';
|
|
|
|
}
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
return { width: percent + '%', backgroundColor: color };
|
|
|
|
},
|
|
|
|
|
|
|
|
rewriteGoogleLink: function () {
|
|
|
|
if (!Core.googleLink) {
|
|
|
|
var anchors = document.getElementById('panorama').getElementsByTagName('a');
|
|
|
|
for (var i = 0; i < anchors.length; i++) {
|
|
|
|
var a = anchors[i];
|
|
|
|
if (a.href.indexOf('maps.google.com/maps') !== -1) {
|
|
|
|
Core.googleLink = a;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
Core.googleLink.title = 'Google Maps'
|
|
|
|
Core.googleLink.href = 'https://maps.google.com/maps'
|
|
|
|
}, 1);
|
2020-05-19 19:01:35 +02:00
|
|
|
}
|
2020-05-20 03:19:53 +02:00
|
|
|
};
|
2020-05-19 19:01:35 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var Util = {
|
|
|
|
EARTH_RADIUS_IN_METER: 6371000,
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
deg2rad: function (deg) {
|
|
|
|
return deg * (Math.PI / 180.0);
|
|
|
|
},
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
calculateDistance: function (position1, position2) {
|
|
|
|
var lat1 = Util.deg2rad(position1.lat);
|
|
|
|
var lng1 = Util.deg2rad(position1.lng);
|
|
|
|
var lat2 = Util.deg2rad(position2.lat);
|
|
|
|
var lng2 = Util.deg2rad(position2.lng);
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var angleCos = Math.cos(lat1) * Math.cos(lat2) * Math.cos(lng2 - lng1) +
|
|
|
|
Math.sin(lat1) * Math.sin(lat2);
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
if (angleCos > 1.0) {
|
|
|
|
angleCos = 1.0;
|
|
|
|
}
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var angle = Math.acos(angleCos);
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
return angle * Util.EARTH_RADIUS_IN_METER;
|
|
|
|
},
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
printDistanceForHuman: function (distance) {
|
|
|
|
if (distance < 1000) {
|
|
|
|
return Number.parseFloat(distance).toFixed(0) + ' m';
|
|
|
|
} else if (distance < 10000) {
|
|
|
|
return Number.parseFloat(distance / 1000).toFixed(2) + ' km';
|
|
|
|
} else if (distance < 100000) {
|
|
|
|
return Number.parseFloat(distance / 1000).toFixed(1) + ' km';
|
|
|
|
} else {
|
|
|
|
return Number.parseFloat(distance / 1000).toFixed(0) + ' km';
|
2020-05-17 19:29:06 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-20 03:19:53 +02:00
|
|
|
};
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.guessMap = new google.maps.Map(document.getElementById('guessMap'), {
|
2020-05-17 19:29:06 +02:00
|
|
|
disableDefaultUI: true,
|
|
|
|
clickableIcons: false,
|
|
|
|
draggableCursor: 'crosshair'
|
|
|
|
});
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.guessMap.fitBounds(guessMapBounds);
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.guessMap.addListener('click', function (e) {
|
|
|
|
if (Core.guessMarker) {
|
|
|
|
Core.guessMarker.setPosition(e.latLng);
|
2020-05-17 19:29:06 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.guessMarker = new google.maps.Marker({
|
|
|
|
map: Core.guessMap,
|
2020-05-17 19:29:06 +02:00
|
|
|
position: e.latLng,
|
2020-05-19 16:18:12 +02:00
|
|
|
clickable: false,
|
|
|
|
draggable: true,
|
|
|
|
label: {
|
|
|
|
color: '#ffffff',
|
|
|
|
fontFamily: 'Roboto',
|
|
|
|
fontSize: '18px',
|
|
|
|
fontWeight: '500',
|
|
|
|
text: '?'
|
|
|
|
}
|
2020-05-17 19:29:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
document.getElementById('guessButton').disabled = false;
|
|
|
|
});
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.panorama = new google.maps.StreetViewPanorama(document.getElementById('panorama'), {
|
2020-05-19 16:18:12 +02:00
|
|
|
disableDefaultUI: true,
|
|
|
|
linksControl: true,
|
|
|
|
showRoadLabels: false
|
|
|
|
});
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.panorama.addListener('position_changed', function () {
|
|
|
|
Core.rewriteGoogleLink();
|
2020-05-19 16:18:12 +02:00
|
|
|
});
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.panorama.addListener('pov_changed', function () {
|
|
|
|
Core.rewriteGoogleLink();
|
2020-05-19 16:18:12 +02:00
|
|
|
});
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.resultMap = new google.maps.Map(document.getElementById('resultMap'), {
|
2020-05-19 16:18:12 +02:00
|
|
|
disableDefaultUI: true,
|
|
|
|
clickableIcons: false,
|
|
|
|
});
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.getNewPosition();
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
document.getElementById('guessButton').onclick = function () {
|
|
|
|
if (!Core.guessMarker) {
|
|
|
|
return;
|
2020-05-19 03:25:42 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var guessedPosition = Core.guessMarker.getPosition();
|
2020-05-19 03:25:42 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
this.disabled = true;
|
|
|
|
Core.guessMarker.setMap(null);
|
|
|
|
Core.guessMarker = null;
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var distance = Util.calculateDistance(Core.realPosition, { lat: guessedPosition.lat(), lng: guessedPosition.lng() });
|
2020-05-19 03:25:42 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
document.getElementById('guess').style.visibility = 'hidden';
|
|
|
|
document.getElementById('result').style.visibility = 'visible';
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var resultBounds = new google.maps.LatLngBounds();
|
|
|
|
resultBounds.extend(Core.realPosition);
|
|
|
|
resultBounds.extend(guessedPosition);
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.resultMap.fitBounds(resultBounds);
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.resultMarkers.real = new google.maps.Marker({
|
|
|
|
map: Core.resultMap,
|
|
|
|
position: Core.realPosition,
|
|
|
|
clickable: true,
|
|
|
|
draggable: false
|
|
|
|
});
|
|
|
|
Core.resultMarkers.guess = new google.maps.Marker({
|
|
|
|
map: Core.resultMap,
|
|
|
|
position: guessedPosition,
|
|
|
|
clickable: false,
|
|
|
|
draggable: false,
|
|
|
|
label: {
|
|
|
|
color: '#ffffff',
|
|
|
|
fontFamily: 'Roboto',
|
|
|
|
fontSize: '18px',
|
|
|
|
fontWeight: '500',
|
|
|
|
text: '?'
|
|
|
|
}
|
|
|
|
});
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.resultMarkers.real.addListener('click', function () {
|
|
|
|
window.open('https://www.google.com/maps/search/?api=1&query=' + Core.realPosition.lat + ',' + Core.realPosition.lng, '_blank');
|
|
|
|
});
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
document.getElementById('distance').innerHTML = Util.printDistanceForHuman(distance);
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var score = Core.calculateScore(distance);
|
|
|
|
var scoreBarProperties = Core.calculateScoreBarProperties(score);
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
document.getElementById('score').innerHTML = Number.parseFloat(score).toFixed(0);
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
var scoreBar = document.getElementById('scoreBar');
|
|
|
|
scoreBar.style.backgroundColor = scoreBarProperties.backgroundColor;
|
|
|
|
scoreBar.style.width = scoreBarProperties.width;
|
|
|
|
}
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
document.getElementById('continueButton').onclick = function () {
|
|
|
|
document.getElementById('scoreBar').style.width = '0';
|
2020-05-20 01:08:01 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.resultMarkers.real.setMap(null);
|
|
|
|
Core.resultMarkers.real = null;
|
|
|
|
Core.resultMarkers.guess.setMap(null);
|
|
|
|
Core.resultMarkers.guess = null;
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
document.getElementById('guess').style.visibility = 'visible';
|
|
|
|
document.getElementById('result').style.visibility = 'hidden';
|
2020-05-19 16:18:12 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.guessMap.fitBounds(guessMapBounds);
|
2020-05-17 19:29:06 +02:00
|
|
|
|
2020-05-20 03:19:53 +02:00
|
|
|
Core.getNewPosition();
|
|
|
|
}
|
|
|
|
})();
|