简体   繁体   English

街景API回调的JS闭包

[英]JS closures for street view API callbacks

The Google maps API offers some methods that make callbacks to user-written methods but with specific parameters, like getPanoramaByLocation() . Google Maps API提供了一些方法,这些方法可以回调用户编写的方法,但是具有特定的参数,例如getPanoramaByLocation() My problem is that I can't reliably associate my requests with the responses. 我的问题是我无法可靠地将我的请求与响应相关联。 The basic call looks like: 基本调用如下所示:

streetViewService.getPanoramaByLocation ( latLng, 10, recordPanorama );

docs: http://code.google.com/apis/maps/documentation/javascript/reference.html#StreetViewService docs: http : //code.google.com/apis/maps/documentation/javascript/reference.html#StreetViewService

I thought that "with" or a closure would help me pass something to recordPanorama, the callback, other than those two preordained parameters, but I seem only to add the information to the context of gPBL() , which of course does not relay it to where it's needed. 我以为“ with”或闭包可以帮助我将某些东西传递给recordPanorama,回调,而不是这两个预先设定的参数,但是我似乎只是将信息添加到gPBL()的上下文中,这当然不会中继到需要的地方。 When I put extra objects into scope with "with", they don't exist in my callback and the arguments passed seem the same. 当我使用“ with”将其他对象放入作用域时,它们在我的回调中不存在,并且传递的参数似乎相同。

Trying to match the geographical coordinates of the request with those of the result is not trivial and I think there would be some errors at large intersections. 尝试将请求的地理坐标与结果的地理坐标进行匹配并不是一件容易的事,我认为在大型交叉路口会出现一些错误。 The only other piece of information I have is the time when the request happens; 我仅有的其他信息是请求发生的时间。 I could tie the queries and responses to each other sort of well by slowing down my requests enough, but isn't there a real way to carry an id to my callback function? 我可以通过充分减慢我的请求的速度来将查询和响应紧密地联系在一起,但是难道没有一种将ID携带给回调函数的真正方法吗?

It seems that you are trying to set some variable X and have it persist into the callback function. 似乎您正在尝试设置一些变量X,并将其保留在回调函数中。 In that case, you could try putting X in the scope of the closure like so: 在这种情况下,您可以尝试将X放在闭包的范围内,如下所示:

for (int i=0;i<9;i++)
{
var x = 10;

magicAjax(URL, function () {
alert(x);
});
}

I haven't tested this code; 我没有测试此代码; I could be wrong. 我可能是错的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM