views:

272

answers:

1

If you go to http://maps.google.com and zoom in until Google runs out of map the viewer automatically transitions to Streetview-mode (at least if Streetview is available at the location you zoomed in on).

I'm mucking around with the Google Maps v3 API. Is there a way to replicate the map-to-streetview effect here? I can enable Streetview just fine by putting streetViewControl: true in the mapOptions, but the user still has to manually drag the stickman onto the map to get Streetview going.

+1  A: 

This will open a panorama at the map center:

var G = google.maps;
var svpContainer = document.getElementById('svp'); // Make sure this div exists
var svp = new G.StreetViewPanorama(svpContainer);
G.event.addListener(map, 'zoom_changed', function(){
    var z = map.getZoom();
    var center = map.getCenter();
    if (z > 15) {
        svp.setPosition(center);
        svp.setVisible(true);
    }

});

Tested over Dallas, TX. Make sure you have a div with id 'svp'

Marcelo
Thanks, that works well. It turns out that any old map comes with its own StreetViewPanorama available through map.getStreetView(). The code can be simplyfied to this (note that I've changed the zoom-level where streetview kicks in to 18): google.maps.event.addListener(map, 'zoom_changed', function(){ sv = map.getStreetView(); if(map.getZoom() > 18){ sv.setPosition(map.getCenter()); sv.setVisible(true); } });
friism
@friism - Ah OK, but that was just from the top of my head. :-) BTW, thatnks to your question, (+1), I found out that StreetView no longer requires Flash, which was the reason why I had never used it before. (I don't have Flash instaled)
Marcelo