从自定义panoProvider返回异步结果

时间:2012-02-16 11:53:10

标签: javascript asynchronous google-maps-api-3 callback

我知道关于这个问题的答案有很多很多,但我找不到适合这种情况的答案。我正在对Google服务进行异步调用,需要返回结果:

function getCustomPanorama(pano,zoom,tileX,tileY) {
    client.getPanoramaById(pano, function(result, status) {
        return {
            location: result.location,
            links: result.links,
            copyright: result.copyright+' BUT GREY',
            tiles: {
                tileSize: result.tiles.tileSize,
                worldSize: result.tiles.worldSize,
                centerHeading: result.tiles.centerHeading,
                getTileUrl: getCustomPanoramaTileUrl
                }
            };
        });
    }

我明白以上是错误的,不会返回,并认为我需要使用回调,但我不明白在哪里。请注意,我无法更改传递给getCustomPanorama的内容。感谢所有的帮助。

更新:完整代码:

var panorama;
var client;

$(document).ready(function() {
    var panoramaOptions = {
        position: new google.maps.LatLng(51.52241608253253, -0.10488510131835938),
        panoProvider: getCustomPanorama
        };
    client = new google.maps.StreetViewService();
    panorama = new google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);
    });

function getCustomPanorama(pano,zoom,tileX,tileY) {
    client.getPanoramaById(pano, function(result, status) {
        return {
            location: result.location,
            links: result.links,
            copyright: result.copyright+' BUT GREY',
            tiles: {
                tileSize: result.tiles.tileSize,
                worldSize: result.tiles.worldSize,
                centerHeading: result.tiles.centerHeading,
                getTileUrl: getCustomPanoramaTileUrl
                }
            };
        });
    }

更新2:

建议肯定是我试图做一些不可能的事情,所以尝试另一种方法,包括预先缓存getPanoramaByID()响应。

2 个答案:

答案 0 :(得分:2)

更改getCustomPanorama以获取回调的额外参数,并传入一个执行结果所需操作的函数:

function getCustomPanorama(pano,zoom,tileX,tileY,callback) {
    client.getPanoramaById(pano, function(result, status) {
        var data = {
          location: result.location,
          links: result.links,
          copyright: result.copyright+' BUT GREY',
          tiles: {
              tileSize: result.tiles.tileSize,
              worldSize: result.tiles.worldSize,
              centerHeading: result.tiles.centerHeading,
              getTileUrl: getCustomPanoramaTileUrl
          }
        };
        callback(data); // call the function and pass in the data you would have returned
    });
}

getCustomPanorama(pano,zoom,tileX,tileY,function(data) {
    // do something with the results of the asynchronous call here        
});

答案 1 :(得分:1)

panoProvider不应该异步调用。这意味着您必须拥有创建预先填充的自定义StreetViewPanorama的所有必要信息。

但如果你真的需要在client.getPanoramaById内调用panoProvider,那么就会有一个非常肮脏的伎俩:

function getCustomPanorama(pano,zoom,tileX,tileY) {
  var resultFromAsyncCall;
  client.getPanoramaById(pano, function(result, status) {
    resultFromAsyncCall = {
        ...
        copyright: result.copyright+' BUT GREY',
        tiles: {
          ... 
          getTileUrl: getCustomPanoramaTileUrl
        }
    };
  });

  while (!resultFromAsyncCall) {
    //wait for result
  }   
  return resultFromAsyncCall;
}

但是,我不鼓励您使用此解决方案。最好尝试重新考虑应用程序的逻辑。

相关问题:Call An Asynchronous Javascript Function Synchronously