我有一个名为Location的类对象,它可以与Google一起使用,以对给定地址进行地址解析。 地理编码请求通过AJAX调用完成,并通过回调处理,一旦响应到达,该回调将启动类成员。Obj构造函数中的AJAX回调
下面是代码:
function Location(address) {
this.geo = new GClientGeocoder();
this.address = address;
this.coord = [];
var geoCallback = function(result) {
this.coord[0] = result.Placemark[0].Point.coordinates[1];
this.coord[1] = result.Placemark[0].Point.coordinates[0];
window.alert("I am in geoCallback() lat: " + this.coord[0] + "; lon: " + this.coord[1]);
}
this.geo.getLocations(this.address, bind(this, geoCallback));
}
Location.prototype.getAddress = function() { return this.address; }
Location.prototype.getLat = function() { return this.coord[0] }
Location.prototype.getLng = function() { return this.coord[1] }
我的问题是:有可能要等待从谷歌之前退出构造的反应如何?
我无法控制AJAX请求,因为它是通过谷歌API制作的。
我想确保this.coord[]
在创建Location obj后正确初始化。
谢谢!
什么是这些属性获得者的好处?您给“this”的每个房产都是公开的。你可以很容易地删除getter并直接使用这些属性(只需创建不同的'Lat'和'Lng'属性而不是'coord'数组)。 – Tomalak 2010-04-19 17:29:32