Google-maps – How to change the street view in google maps 3

google mapsgoogle-maps-api-3

Goal: Get the TEXT address and then display the street view and map view at the same time

Ref Site:

http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html

My site:
http://www.iamvishal.com/dev/property/P2154 (pls click the map view to see the map)

Problem: I am able to display the map and the address correctly but instreet view does not change. Any idea why ?

My js variable address hold the text address in this case "323235 Balmoral Terrace Heaton Newcastle Upon Tyne"

function initialize() 
{
var fenway = new google.maps.LatLng(42.345573,-71.098326);
var mapOptions = {
    center: fenway,
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(
document.getElementById("map_canvas"), mapOptions);

var panoramaOptions = {
    position: fenway,
    pov: {
         heading: 34,
         pitch: 10,
         zoom: 1
         }
   };

var panorama = new  google.maps.StreetViewPanorama(document.getElementById("pano"), 
panoramaOptions);

map.setStreetView(panorama);

var geocoderTwo = new google.maps.Geocoder();

geocoderTwo.geocode({"address":address},function(results, status)
{
   if (status == google.maps.GeocoderStatus.OK) 
   {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker
      ({
        map: map,
        position: results[0].geometry.location
      });
   }
else
{
  alert("Geocode was not successful for the following reason: " + status);
}
}


);


}

Best Answer

Here's how I've created a streetview before:

function createStreetMap(strMapCanvasID, yourLatLng)
{
    var panorama;

    //once the document is loaded, see if google has a streetview image within 50 meters of the given location, and load that panorama
    var sv = new google.maps.StreetViewService();

    sv.getPanoramaByLocation(yourLatLng, 50, function(data, status) {
        if (status == 'OK') {
            //google has a streetview image for this location, so attach it to the streetview div
            var panoramaOptions = {
                pano: data.location.pano,
                addressControl: false,
                navigationControl: true,
                navigationControlOptions: {
                    style: google.maps.NavigationControlStyle.SMALL
                }
            }; 
            var panorama = new google.maps.StreetViewPanorama(document.getElementById(strMapCanvasID), panoramaOptions);


            // lets try and hide the pegman control from the normal map, if we're displaying a seperate streetview map
            objCreatedMap.setOptions({
                streetViewControl: false
            });
        }
        else{
            //no google streetview image for this location, so hide the streetview div
            $('#' + strMapCanvasID).parent().hide();
        }
    });
}

Update: and you could call this function directly from within your existing code (I've amended the function to use a LatLng rather than individual latitude and longitude values:

if (status == google.maps.GeocoderStatus.OK) 
   {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker
      ({
        map: map,
        position: results[0].geometry.location
      });

      createStreetMap('yourStreetViewDiv', results[0].geometry.location);
   }
Related Topic