当用户从UI组件单击“保存”按钮时,我想完成此任务库。 save方法按以下顺序调用服务。
向UI中的数据传递添加其他信息。其中一个信息是基于ui数据获取地理定位(lng + lat)信息。我正在使用另一项服务,该服务会对Google maps API进行HTTP调用
getLocation(address: string): Location {
const urlPath = GoogleMapAPI.url + "json?address=" + encodeURIComponent(address) + "&key=" + GoogleMapAPI.key;
let location: Location = null;
//console.log(urlPath);
const results = this.http
.get(urlPath)
.map(res => res.json());
results.subscribe(result => {
if (result.status !== "OK") { throw new Error("unable to geocode address"); }
//let location = new Location();
location = new Location();
location.address = result.results[0].formatted_address;
location.latitude = result.results[0].geometry.location.lat;
location.longitude = result.results[0].geometry.location.lng;
//console.log("GeocodingService:geocode", address, location);
});
return location;
}
保存数据
addRace(race: Race): firebase.Promise<any> {
race.created_ts = race.updated_ts = moment().format();
// additional information
this.setRaceInfo(race);
// get geo location
let location = this.geoService.getLocation(Race.formatFullAddress(race));
if (location != null){
race.latitude = location.latitude;
race.longitude = location.longitude;
race.address = location.address;
}
//save
return this.racesList.push(race);
}
控制台日志输出
save Object {name: "Test", date: "2014-11-07", city: "New York", country: "United States"…}
GeocodingService:geocode New York, New York, United States Location {address: "New York, NY, USA", latitude: 40.7127837, longitude: -74.0059413}
我能够保存数据但是在调用save方法之前我无法获取地理位置数据。调用save方法后返回的地理位置值。我错过了什么吗?