街景服务:将街景视图添加到搜索结果中

时间:2012-10-25 10:13:56

标签: google-maps-api-3 google-street-view

你需要一些关于使用街景的想法/建议, 我正在获取使用地点Api按类型实施搜索的地方详细信息 下面是我的搜索地方代码

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>');

    }
});

我在我的网页上显示结果..我想实现的是为每个地址添加街景。任何建议,将不胜感激。

1 个答案:

答案 0 :(得分:1)

您有两个选择:

  1. 只需使用Static StreetView-Image-API提供的图片即可。但 如果这个地方没有可用的图像,你会得到一个 误差图像。
  2. 使用getPanoramaByLocation()请求给定位置的StreetView详细信息,这可以让您在没有可用于此地点的图片的情况下处理该情况,但需要进一步请求,并且您还可以加载全景。

  3. <强> <edit>

    根据要求,案例#2可以像这样处理(以下代码是您当前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)
    
    
        }
    }