streetmapme/script.js

142 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-08-07 07:11:59 -06:00
'use strict';
2017-08-07 08:24:58 -06:00
/* global navigator google */
2017-08-07 08:15:52 -06:00
var pano, newLoc;
2017-08-07 07:11:59 -06:00
2017-08-07 08:17:59 -06:00
// Create panorama
function init(){
2017-08-07 10:07:39 -06:00
pano = new google.maps.StreetViewPanorama(
document.getElementsByTagName('main')[0], {
panControl: false,
zoomControl: false,
addressControl: false,
linksControl: false,
motionTracking: false,
motionTrackingControl: false
}
);
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) {
// Ensure that the location hasn't changed (or this is the initial setting)
if ( newLoc == null || loc.tim===newLoc.tim ) {
2017-08-07 08:24:58 -06:00
if (!sv) { var sv=new google.maps.StreetViewService(); }
2017-08-07 08:01:45 -06:00
sv.getPanorama({
location: {
lat: loc.lat,
lng: loc.lon
},
radius: rad
}, function(data,status){ switch (status){
// 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;
// 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;
// Error
default:
2017-08-07 10:07:39 -06:00
console.error(new Error(
'❌️ Street view not available: '+status
).message);
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-08-07 09:50:05 -06:00
// Panorama hasn't loaded
if ( typeof pano == 'undefined' ){
// Wait one second and try again
setTimeout(updateStreetView(loc),1000);
}
// Panorma has loaded
else {
2017-08-07 10:07:39 -06:00
// Get streetview data
2017-08-07 08:17:59 -06:00
getStreetViewData(loc, 2, function(data){
2017-08-07 10:07:39 -06:00
// Calculate bearing
var brng;
if (loc.spd>1){
brng = loc.dir;
}
else { //https://stackoverflow.com/a/26609687/3006854
brng = Math.atan2( loc.lat-data.location.latLng.lat(), loc.lon-data.location.latLng.lng() );
2017-09-25 16:57:44 -06:00
brng = 90-(brng*(180/Math.PI))%360;
2017-08-07 10:07:39 -06:00
}
// Set panorama
2017-08-07 08:38:59 -06:00
pano.setPano(data.location.pano);
2017-08-07 08:17:59 -06:00
pano.setPov({
pitch: 0,
2017-08-07 10:07:39 -06:00
heading: brng
2017-08-07 08:17:59 -06:00
});
2017-08-07 08:15:52 -06:00
});
2017-08-07 08:17:59 -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
}