streetmapme/script.js

96 lines
2.0 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 09:50:05 -06:00
pano = new google.maps.StreetViewPanorama(document.getElementsByTagName('main')[0], {
2017-08-07 08:17:59 -06:00
panControl: false,
zoomControl: false,
addressControl: false,
linksControl: false,
motionTracking: false,
motionTrackingControl: false
});
}
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:
console.error(new Error('❌️ Street view not available: '+status).message);
} });
}
}
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 08:17:59 -06:00
// Set panorama
getStreetViewData(loc, 2, function(data){
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 08:38:59 -06:00
heading: (loc.spd>1)?loc.dir:Math.atan((loc.lon-data.location.latLng.lng())/(loc.lat-data.location.latLng.lat()))*(180/Math.PI)
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 07:11:59 -06:00
// Track GPS location
2017-08-07 08:01:45 -06:00
if (!navigator.geolocation){ /* Show error */ }
2017-08-07 07:11:59 -06:00
else { navigator.geolocation.watchPosition(
// 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
function() {
//TODO: Show error
},
// Options
{ enableHighAccuracy:true }
);
2017-08-07 08:15:52 -06:00
}