Search code examples
google-maps-api-3google-street-view

Street View Service: Adding street view to the search results


Hi need some idea/suggestion on using the street view, i am getting place details implementing a search by type using the places Api below is my search places code

function SearchData(type){

    $('#placedata').empty();
        var myLatlng = new google.maps.LatLng(parseFloat(searchLatitute), parseFloat(searchLongitute));
        map = new google.maps.Map(document.getElementById('map_canvas'), {
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: myLatlng,
                    zoom: 10});
        var request = {location: myLatlng,rankBy: google.maps.places.RankBy.DISTANCE,types: [type]};
        var service = new google.maps.places.PlacesService(map);
        service.search(request, callback);
  }

  function callback(results, status,pagination) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
        var place = results[i];
        createMarker(place);

  function createMarker(place) {
        var placeLoc = place.geometry.location;
        var marker = new google.maps.Marker({map: map,zIndex: 100,position: place.geometry.location});

    var request = {reference: place.reference};
    service = new google.maps.places.PlacesService(map);
    service.getDetails(request, function(details, status) {//alert("address  "+details.formatted_address);
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        $('#placedata').append('<tr><td><a href='+details.url+'>'+details.name+'</a></td></tr>');

    }
});

i am displaying the result in my webpage..what i wanna implement is to add a street view to each address. Any suggestions would be appreciated.


Solution

  • You have 2 options:

    1. Simply use the image provided by the Static StreetView-Image-API. But if there is no image available for this place you'll get a error-image.
    2. request StreetView-details for the given location by using getPanoramaByLocation(), what allows you to handle the case when no Image is available for this place, but requires a further request, and you'll also be able to load a Panorama.

    <edit>

    As requested, the case #2 can be handled like this(the following code is a replacement for your current service.getDetails()-callback):

    function(details, status) {
        if (status == google.maps.places.PlacesServiceStatus.OK) {
    
          //create streetViewService-object once
          if(!window.streetViewService)window.streetViewService 
            = new google.maps.StreetViewService;   
    
    
            (function(d){
              //append the tr to the table, note the additional td and div
              //thats where the panorama will be placed later
              var target=$('<tr><td><a href='+
                            d.url+'>'+d.name+
                           '</a></td><td><div/></td></tr>');
    
              target.appendTo('#placedata');
    
              //get the panorama
              window.streetViewService
                .getPanoramaByLocation(d.geometry.location,
                                       50,
                                       function(r,status){
                                       //check if there was an result
                                       if(status==google.maps.StreetViewStatus.OK){
                                        //create the panorama
                                        new google.maps
                                          .StreetViewPanorama(
                                              $('td:last div:last',target)
                                                .css({width:200,height:200})[0],
                                             {pano:r.location.pano});
                                      }
                                      else{
                                        $('td:last div:last',target)
                                         .text('no panorama available');
                                      }
                                    });
            })(details)
    
    
        }
    }