Skip to content

Instantly share code, notes, and snippets.

@ifnull
Created July 24, 2017 04:26
Show Gist options
  • Save ifnull/a19ae07d6360771cd7dbff990bc1634f to your computer and use it in GitHub Desktop.
Save ifnull/a19ae07d6360771cd7dbff990bc1634f to your computer and use it in GitHub Desktop.
Get POV heading for StreetViewPanorama
function initialize() {
// initialize the geocoder API functions. We need this to convert the address to a geolocation (GPS coordinates)
var geocoder = new google.maps.Geocoder();
// then we call the geocode function with the address we want to use as parameter
geocoder.geocode( { 'address': '20 River Terrace, New York, NY, United States' }, function(results, status) {
// set the lookTo var to contain the coordinates of the address entered above
var lookTo = results[0].geometry.location;
// if the address is found and the geocoder function returned valid coordinates
if (status == google.maps.GeocoderStatus.OK) {
// set the options for the panorama view
var panoOptions = {
position: lookTo,
panControl: false,
addressControl: false,
linksControl: false,
zoomControlOptions: false
};
// initialize a new panorama API object and point to the element with ID streetview as container
var pano = new google.maps.StreetViewPanorama(document.getElementById('streetview'),panoOptions);
// initialize a new streetviewService object
var service = new google.maps.StreetViewService;
// call the "getPanoramaByLocation" function of the Streetview Services to return the closest streetview position for the entered coordinates
service.getPanoramaByLocation(pano.getPosition(), 50, function(panoData) {
// if the function returned a result
if (panoData != null) {
// the GPS coordinates of the streetview camera position
var panoCenter = panoData.location.latLng;
// this is where the magic happens!
// the "computeHeading" function calculates the heading with the two GPS coordinates entered as parameters
var heading = google.maps.geometry.spherical.computeHeading(panoCenter, lookTo);
// now we know the heading (camera direction, elevation, zoom, etc) set this as parameters to the panorama object
var pov = pano.getPov();
pov.heading = heading;
pano.setPov(pov);
// set a marker on the location we are looking at, to verify the calculations were correct
var marker = new google.maps.Marker({
map: pano,
position: lookTo
});
} else {
// no streetview found :(
alert('not found');
}
});
} else {
// there were no coordinates found for the address entered (or the address was not found)
alert('Could not find your address :(');
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment