This is a snippet from the series described in Playing around with maps and streetview, which will use the service to find an appropriate panorama, given a location, and set the intial heading and pitch so that it's orientated towards the marker
When you enable a panorama, by default it will be orientated towards a default view, which is not likely going to be looking at the location you have generated the streetview for. Here's how to bring up the initial view looking in the direction of your marker (which is plotted on maps). All the preparatory setup has been done and the entire app is available on github - so this will just go through the technique for getting the right camera orientation.
You can use the StreetViewService to directly access the streetview data, passing to it a few options. Here I'm looking for outside views within some number of meters (50) from my map marker. It's asynchronous of course, so I'm returning a promise.
The callback returns a result and status. Assuming we found something, from the location the default image location (cameraPosition), I can calculate the heading (the angle between) it and the marker using one of the maps geometry functions - this gives a heading
which I'll save for later, and also apply to the active panorama right now
and I'll pass the handling of errors back to the caller
My test data consists of various MacDonald's around the world. The calculation above sets the initial view to be looking at each one - just like this.
Services > Desktop Liberation - the definitive resource for Google Apps Script and Microsoft Office automation > Ephemeral Exchange > Playing around with maps and streetview >