从自定义 panoProvider 返回异步结果

发布于 2025-01-06 08:56:52 字数 1766 浏览 4 评论 0原文

我知道这个问题有很多很多答案,但我一直找不到适用于这种情况的答案。我正在对 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() 响应的方法。

I know there are many, many answers to this question on SO, but I haven't been able to find one that applies in this case. I'm making an asynchronous call to a Google service and need to return the result:

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

I understand that the above is wrong and will not return, and think I need to use callbacks, but I don't understand where. Note that I can't change what is passed to getCustomPanorama. All help gratefully received.

UPDATE: Full code:

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

UPDATE 2:

Suggestions are definitely that I'm trying to do something impossible, so trying another approach involving pre-caching the getPanoramaByID() responses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2025-01-13 08:56:52

更改 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        
});

Change getCustomPanorama to take an extra parameter for the callback and pass in a function that does what you needed to do with the result:

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        
});
欲拥i 2025-01-13 08:56:52

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

但是,如果您确实需要在 panoProvider 内部调用 client.getPanoramaById,那么有一个非常肮脏的技巧:

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;
}

但是,我不鼓励< /em> 你不要使用这个解决方案。最好尝试重新思考应用程序的逻辑。

相关问题:同步调用异步 Javascript 函数

panoProvider is not supposed to be called asynchronously. That means you have to have all necessary information for creating custom StreetViewPanoramas pre-populated.

But if you really need to call client.getPanoramaById inside of panoProvider then there is a very dirty trick:

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;
}

BUT, I discourage you from using this solution. Better try to re-think logic of your application.

Related question: Call An Asynchronous Javascript Function Synchronously

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文