streetmapme/script.js

91 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-08-07 07:11:59 -06:00
'use strict';
2017-08-07 08:15:52 -06:00
/* global navigator googlemaps $ */
var pano, newLoc;
const main = document.getElementsByTagName('main')[0];
2017-08-07 07:11:59 -06:00
2017-08-07 08:17:59 -06:00
// Create panorama
function init(){
pano = new googlemaps.StreetViewPanorama(main, {
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 ) {
if (!sv) { var sv=new googlemaps.StreetViewService(); }
sv.getPanorama({
location: {
lat: loc.lat,
lng: loc.lon
},
radius: rad
}, function(data,status){ switch (status){
// Success
case googlemaps.StreetViewStatus.OK:
cb(data);
break;
// No results in that radius
case googlemaps.StreetViewStatus.ZERO_RESULTS:
// 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 08:17:59 -06:00
// Wait for panorama
if ( typeof pano != 'undefined' ){
// Set panorama
getStreetViewData(loc, 2, function(data){
pano.setPano(data.location.pano);
pano.setPov({
pitch: 0,
// Point towards users's location from street
heading: Math.atan((loc.lon-data.location.latLng.lng())/(loc.lat-data.location.latLng.lat()))*(180/Math.PI)
});
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,
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
}