gpt4 book ai didi

javascript - 从自定义 panoProvider 返回异步结果

转载 作者:行者123 更新时间:2023-11-29 20:11:39 25 4
gpt4 key购买 nike

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

最佳答案

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

关于javascript - 从自定义 panoProvider 返回异步结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9310819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com