I want to accomplish this task base when user click save button from UI component. The save method call the service to accomplished in the following order.
Add additional information to data pass from UI. One of the information is get geolocation(lng+lat) information base on ui data. I'm using another service, which makes an HTTP call to Google maps API
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; }
Save the data
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); }
Console log output
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}
I'm able to save the data but I couldn't get the geolocation data before the save method is called. The geolocation value return after the save method is called. Am I missing something?