2017-08-07 07:11:59 -06:00
|
|
|
'use strict';
|
2017-09-25 17:18:22 -06:00
|
|
|
/* global $ navigator google */
|
2017-08-07 08:15:52 -06:00
|
|
|
|
2017-09-25 17:18:22 -06:00
|
|
|
var newLoc;
|
2017-08-07 08:17:59 -06:00
|
|
|
|
2017-08-07 08:01:45 -06:00
|
|
|
// Get street view imagery
|
|
|
|
function getStreetViewData(loc,rad,cb) {
|
2017-09-25 17:18:22 -06:00
|
|
|
|
2017-08-07 08:01:45 -06:00
|
|
|
// Ensure that the location hasn't changed (or this is the initial setting)
|
|
|
|
if ( newLoc == null || loc.tim===newLoc.tim ) {
|
2017-09-25 17:18:22 -06:00
|
|
|
|
|
|
|
// Create service if it doesn't exist yet
|
2017-08-07 08:24:58 -06:00
|
|
|
if (!sv) { var sv=new google.maps.StreetViewService(); }
|
2017-09-25 17:18:22 -06:00
|
|
|
|
|
|
|
// Retrieve data
|
2017-08-07 08:01:45 -06:00
|
|
|
sv.getPanorama({
|
|
|
|
location: {
|
|
|
|
lat: loc.lat,
|
|
|
|
lng: loc.lon
|
|
|
|
},
|
|
|
|
radius: rad
|
|
|
|
}, function(data,status){ switch (status){
|
2017-09-25 17:18:22 -06:00
|
|
|
|
2017-08-07 08:01:45 -06:00
|
|
|
// Success
|
2017-08-07 08:24:58 -06:00
|
|
|
case google.maps.StreetViewStatus.OK:
|
2017-08-07 08:01:45 -06:00
|
|
|
cb(data);
|
|
|
|
break;
|
2017-09-25 17:18:22 -06:00
|
|
|
|
2017-08-07 08:01:45 -06:00
|
|
|
// No results in that radius
|
2017-08-07 08:24:58 -06:00
|
|
|
case google.maps.StreetViewStatus.ZERO_RESULTS:
|
2017-08-07 08:01:45 -06:00
|
|
|
// Try again with a bigger radius
|
|
|
|
getStreetViewData(loc,rad*2,cb);
|
|
|
|
break;
|
2017-09-25 17:18:22 -06:00
|
|
|
|
2017-08-07 08:01:45 -06:00
|
|
|
// Error
|
|
|
|
default:
|
2017-08-07 10:07:39 -06:00
|
|
|
console.error(new Error(
|
|
|
|
'❌️ Street view not available: '+status
|
|
|
|
).message);
|
2017-09-25 17:18:22 -06:00
|
|
|
|
2017-08-07 08:01:45 -06:00
|
|
|
} });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 08:17:59 -06:00
|
|
|
// Update street view image
|
2017-08-07 08:15:52 -06:00
|
|
|
function updateStreetView(loc){
|
|
|
|
|
2017-09-25 17:18:22 -06:00
|
|
|
// Calculate bearing between user and position of streetview image
|
|
|
|
// https://stackoverflow.com/a/26609687/3006854
|
|
|
|
function getBearing(userLoc, imageLoc) {
|
|
|
|
return 90-(
|
|
|
|
Math.atan2( userLoc.lat-imageLoc.latLng.lat(), userLoc.lon-imageLoc.latLng.lng() )
|
|
|
|
* (180/Math.PI) ) % 360;
|
2017-08-07 09:50:05 -06:00
|
|
|
}
|
|
|
|
|
2017-09-25 17:40:29 -06:00
|
|
|
// Get dimensions for sv request (images proportional to element up to 640x640)
|
|
|
|
function getDimensions(element) {
|
|
|
|
|
|
|
|
// Window is smaller than max
|
|
|
|
if ( element.width()<640 && element.height()<640 ){
|
|
|
|
return element.width()+'x'+element.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Width must be made proportional to 640
|
|
|
|
else if (element.width()>element.height()) {
|
|
|
|
return '640x'+element.height()*640/element.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Height must be made proportional to 640
|
|
|
|
else {
|
|
|
|
return element.width()*640/element.height()+'x640';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-25 17:18:22 -06:00
|
|
|
// Get streetview data
|
|
|
|
getStreetViewData(loc, 2, function(data){
|
|
|
|
console.log('https://maps.googleapis.com/maps/api/streetview?'+
|
2017-09-25 17:40:29 -06:00
|
|
|
'size='+ getDimensions($(window)) +
|
2017-09-25 17:18:22 -06:00
|
|
|
'&location='+ data.location.latLng.lat() +','+ data.location.latLng.lng() +
|
|
|
|
'&fov=90' + // Inclination
|
|
|
|
// Show direction if moving, point to user if stationary
|
|
|
|
'&heading='+ ( (loc.spd>2)? loc.dir: getBearing(loc,data.location) ).toString() +
|
|
|
|
'&key=AIzaSyAaDEDFAbk7Iefh9y61wuooDH2kWHRT_k8');
|
2017-08-07 10:07:39 -06:00
|
|
|
|
2017-09-25 17:18:22 -06:00
|
|
|
// Set image to streetview
|
|
|
|
$('main img').attr('src','https://maps.googleapis.com/maps/api/streetview?'+
|
|
|
|
'size='+ $('main img').width() +'x'+ $('main img').height() +
|
|
|
|
'&location='+ data.location.latLng.lat() +','+ data.location.latLng.lng() +
|
|
|
|
'&fov=90' + // Inclination
|
|
|
|
// Show direction if moving, point to user if stationary
|
|
|
|
'&heading='+ ( (loc.spd>2)? loc.dir: getBearing(loc,data.location) ).toString() +
|
|
|
|
'&key=AIzaSyAaDEDFAbk7Iefh9y61wuooDH2kWHRT_k8'
|
|
|
|
);
|
2017-08-07 08:17:59 -06:00
|
|
|
|
2017-09-25 17:18:22 -06:00
|
|
|
});
|
2017-08-07 08:15:52 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-07 16:03:15 -06:00
|
|
|
// Check for geolocation
|
2017-08-07 16:01:12 -06:00
|
|
|
if (!navigator.geolocation){
|
|
|
|
alert("Geolocation not available!");
|
2017-09-25 16:57:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Has geolocation
|
|
|
|
else {
|
2017-08-07 16:03:15 -06:00
|
|
|
|
|
|
|
// Track geolocation
|
|
|
|
navigator.geolocation.watchPosition(
|
2017-08-07 07:11:59 -06:00
|
|
|
|
|
|
|
// Got location
|
|
|
|
function(pos) {
|
2017-08-07 08:15:52 -06:00
|
|
|
newLoc = {
|
|
|
|
lat: pos.coords.latitude,
|
|
|
|
lon: pos.coords.longitude,
|
2017-08-07 08:38:59 -06:00
|
|
|
spd: pos.coords.speed,
|
|
|
|
dir: pos.coords.heading,
|
2017-08-07 08:15:52 -06:00
|
|
|
tim: new Date()
|
|
|
|
};
|
|
|
|
updateStreetView(newLoc,10);
|
2017-08-07 07:11:59 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
// Got error
|
2017-08-07 16:05:41 -06:00
|
|
|
function(err) {
|
2017-08-07 16:23:08 -06:00
|
|
|
console.error(err.message);
|
2017-09-25 16:57:44 -06:00
|
|
|
|
|
|
|
// Permission denied
|
2017-08-07 16:23:08 -06:00
|
|
|
if (err.code==1) {
|
|
|
|
alert("You can't use this app without granting permission to access your location");
|
|
|
|
}
|
2017-09-25 16:57:44 -06:00
|
|
|
|
|
|
|
// Location not available
|
2017-08-07 16:23:08 -06:00
|
|
|
else if (err.code==2) {
|
|
|
|
alert("Location data not available.");
|
|
|
|
}
|
2017-09-25 16:57:44 -06:00
|
|
|
|
|
|
|
// Timeout
|
2017-08-07 16:23:08 -06:00
|
|
|
else if (err.code==3) {
|
|
|
|
alert("Timed out trying to determine location.");
|
|
|
|
}
|
2017-09-25 16:57:44 -06:00
|
|
|
|
|
|
|
// Other error
|
2017-08-07 16:23:08 -06:00
|
|
|
else {
|
|
|
|
alert("An unknown error occured while trying to determine location.");
|
|
|
|
}
|
2017-09-25 16:57:44 -06:00
|
|
|
|
2017-08-07 07:11:59 -06:00
|
|
|
},
|
|
|
|
|
|
|
|
// Options
|
|
|
|
{ enableHighAccuracy:true }
|
|
|
|
|
|
|
|
);
|
2017-08-07 08:15:52 -06:00
|
|
|
}
|