Search code examples
inputmapsgoogle-street-view

Input address and display Google Street View location


I'm trying to allow people to 'by-pass' the traditional Google Maps, and upon input of the address, to immediately display the Street View Location. (I don't want any interaction with the maps pegman or markers etc - just enter address & display street view).

Any help would be much appreciated.

Below is code I've been try to hack at to make these results. But I'm new to it and a bit confused.

var map;
  var berkeley = new google.maps.LatLng(-29.859515,30.998577);
  var sv = new google.maps.StreetViewService();

  var panorama;

  function initialize() {

    panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'));

    // Set up the map
    var mapOptions = {
      center: berkeley,
      zoom: 16,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      streetViewControl: false
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);






         var input = document.getElementById('searchTextField');
    var autocomplete = new google.maps.places.Autocomplete(input);

    autocomplete.bindTo('bounds', map);

    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
      map: map
    });



    google.maps.event.addListener(autocomplete, 'place_changed', function() {

      infowindow.close();
      marker.setVisible(false);
      input.className = '';
      var place = autocomplete.getPlace();
      if (!place.geometry) {
        // Inform the user that the place was not found and return.
        input.className = 'notfound';
        return;
      }

      // If the place has a geometry, then present it on a map.
      if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
      } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
      }




    //edit the doing var <!-- Edit this for auto marking to affect street view --> 

      var markerPanoID = data.location.pano;
        // Set the Pano to use the passed panoID
        panorama.setPano(markerPanoID);
        panorama.setPov({
          heading: 270,
          pitch: 0
        });
        panorama.setVisible(true);




      var address = '';
      if (place.address_components) {
        address = [
          (place.address_components[0] && place.address_components[0].short_name || ''),
          (place.address_components[1] && place.address_components[1].short_name || ''),
          (place.address_components[2] && place.address_components[2].short_name || '')
        ].join(' ');
      }

      infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
      infowindow.open(map, marker);
    });



    // getPanoramaByLocation will return the nearest pano when the
    // given radius is 50 meters or less.
    google.maps.event.addListener(map, 'click', function(event) {
        sv.getPanoramaByLocation(event.latLng, 50, processSVData);
    });

  }

  function processSVData(data, status) {
    if (status == google.maps.StreetViewStatus.OK) {
      var marker = new google.maps.Marker({
        position: data.location.latLng,
        map: map,
        title: data.location.description
      });

      panorama.setPano(data.location.pano);
      panorama.setPov({
        heading: 270,
        pitch: 0
      });
      panorama.setVisible(true);





      google.maps.event.addListener(marker, 'click', function() {

        var markerPanoID = data.location.pano;
        // Set the Pano to use the passed panoID
        panorama.setPano(markerPanoID);
        panorama.setPov({
          heading: 270,
          pitch: 0
        });
        panorama.setVisible(true);
      });
      } else {
      alert('Street View data not found for this location.');
    }
  }



  google.maps.event.addDomListener(window, 'load', initialize);

Solution

  • The below link might answer your question. Its describes:

    • How to get Google Map marker and Street View from an address input.
    • It also computes point of view / yaw so that you are facing the correct street view angle.

    Description: http://www.jaycodesign.co.nz/js/using-google-maps-to-show-a-streetview-of-a-house-based-on-an-address/

    Demo: http://www.jaycodesign.co.nz/wp-content/uploads/demos/StreetView/

    Update as above links are broken. Consider the following SO question & answer: Use Google Maps API to display Street View of a house based on an address

    I've not tested it but its more current.