Created
July 24, 2017 04:26
-
-
Save ifnull/a19ae07d6360771cd7dbff990bc1634f to your computer and use it in GitHub Desktop.
Get POV heading for StreetViewPanorama
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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