Javascript 从自定义提供程序返回异步结果
我知道这个问题有很多很多答案,但我还没有找到一个适用于这种情况的答案。我正在对Google服务进行异步调用,需要返回结果:Javascript 从自定义提供程序返回异步结果,javascript,asynchronous,google-maps-api-3,callback,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: r
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
}
};
});
}
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
});
我知道上面的内容是错误的,不会返回,我认为我需要使用回调,但我不知道在哪里。请注意,我无法更改传递给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) {
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
}
};
});
}
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
});
不应异步调用提供程序。这意味着您必须拥有创建自定义
StreetViewPanorama
s预填充的所有必要信息
但是如果您确实需要在
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;
}
但是,我不鼓励您使用此解决方案。最好重新思考应用程序的逻辑
相关问题:谢谢Richard,但这就是问题所在-我无法编辑传递给getCustomPanorama的内容,因为它是从Google的Maps API中调用的函数。@stml那么,是Google Maps API对您传回的数据做了一些处理吗?有没有一个函数可以自己调用并传递数据?我已经用上面的完整代码进行了更新,以便您可以看到它是如何调用的。我尝试过修改调用,但没有成功…
client.getPanoramaById
是Google Maps API提供的方法。但是getCustomPanorama()
似乎在您的控制之下。getCustomPanorama()是在另一个Google函数中调用的,它定义了一个自定义的街景全景。您可以发布调用您的getCustomPanorama()
的代码吗?谢谢-理解这个解决方案,但它肯定不可行。我知道我正在做一些出乎意料的事情,所以感谢可能没有好的答案…@yatskevich-你的答案有没有一个有效的答案?