【发布时间】:2010-04-19 17:08:06
【问题描述】:
我有一个名为 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] }
我的问题是:在退出构造函数之前可以等待 Google 的响应吗?
我无法控制 AJAX 请求,因为它是通过 Google API 发出的。
我想确保在创建 Location obj 后正确初始化 this.coord[]。
谢谢!
【问题讨论】:
-
这些属性获取器有什么用?您给
this的每个属性都是公开的。您可以轻松删除 getter 并直接使用属性(只需创建不同的Lat和Lng属性,而不是coord数组)。
标签: javascript ajax constructor geocoding gdata-api