我有一个名为 Location 的类对象,它与 Google 一起使用,以便对给定地址进行地理编码。地理编码请求是通过 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 请求,因为它是通过 Google API 生成的。
我想确保this.coord[]
在创建 Location obj 后正确初始化。
谢谢!