streetmapme/script.js

116 lines
2.3 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() );
brng = 450-(brng*(180/Math.PI)+360)%360;
}
// 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 07:11:59 -06:00
// Track GPS location
2017-08-07 16:01:12 -06:00
if (!navigator.geolocation){
alert("Geolocation not available!");
}
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
}