[GIS] Initializing Google Maps Streetview object looking at the position

google-maps-apijavascript

I have a streetview included in my web site using google API to load it.

var theloc = new google.maps.LatLng(lat, lon);
var panoramaOptions = {
  addressControl: false,
  position: theloc,
  pov: {
    heading: 0, /* North */
    pitch: -40, /* Look down at angle 40deg */
    zoom: 0
  },
  visible: true
};
var streetview = new google.maps.StreetViewPanorama(document.getElementById("streetview"),
                     panoramaOptions);

This works, but I have one issue:

I am interested in loading the view by looking at the position I refer to, but when loading this it always points to north, and the location I specify is not necessarily in view.

If pointing to maps.google.com using URL to open a street view direclty in Google, I can simply omit the heading part of the panorama options parameter (cbp) like this:

'                                         \/ The heading value space                        '
https://maps.google.com/maps?layer=c&cbp=0,,,,40&cbll=58.9582956492561,5.62897047851361

This causes Google to automatically center the view on showing the position specified in cbll, and I don't need to know what heading to use.

Is it possible to do the same in the javascript API? If I try to omit the heading in the pov object, or initializing heading with null, the street view fails to load. I have also tried to set heading=-1, but that just points always to north. I also tried to not set the pov object at all, but that also always points to north (plus I don't get to set vertical angle 40 degrees down like I want).

Best Answer

As commented by blah238 this was already answered on Stack Overflow in this question:

Getting the POV for Google StreetView API

So based on this answer, this is possible to do, although not as straight-forward as I hoped it would be...