从自定义 panoProvider 返回异步结果
我知道这个问题有很多很多答案,但我一直找不到适用于这种情况的答案。我正在对 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
getCustomPanorama
以获取回调的额外参数,并传入一个函数来执行您需要对结果执行的操作: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:panoProvider
不应该被异步调用。这意味着您必须预先填充创建自定义StreetViewPanorama
所需的所有信息。但是,如果您确实需要在
panoProvider
内部调用client.getPanoramaById
,那么有一个非常肮脏的技巧:但是,我不鼓励< /em> 你不要使用这个解决方案。最好尝试重新思考应用程序的逻辑。
相关问题:同步调用异步 Javascript 函数
panoProvider
is not supposed to be called asynchronously. That means you have to have all necessary information for creating customStreetViewPanorama
s pre-populated.But if you really need to call
client.getPanoramaById
inside ofpanoProvider
then there is a very dirty trick: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