I am new to Angular 2 and I have a question regarding invoking a child method from the current constructor.
Is it possible to call the getPosition method from the constructor? I attempted to do so, but encountered an exception stating "getPosition is not a function".
import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { Platform } from 'ionic-angular';
import { Q } from 'q';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
private map;
constructor(public navCtrl: NavController, platform: Platform, public alertCtrl: AlertController) {
platform.ready().then(() => {
try {
let div = document.getElementById("map_canvas");
// Initialize the map view
this.map = (<any>window).plugin.google.maps.Map.getMap(div);
// Wait until the map is ready status.
this.map.addEventListener((<any>window).plugin.google.maps.event.MAP_READY, function() {
this.getPosition().then(data => {
let GOOGLE = new (<any>window).plugin.google.maps.LatLng(data.latitude, data.longitude);
this.map.setCenter(GOOGLE);
}).catch(err => {
alert(err);
});
});
} catch(err) {
alert(err);
}
}).catch(err => {
alert(err);
});
}
getPosition() {
let deferred = Q.defer();
try {
this.map.getMyLocation(location => {
deferred.resolve( {
latitude: location.latLng.lat,
longitude: location.latLng.lng
});
}, err => {
deferred.reject(err);
});
} catch(err) {
deferred.rejec(err);
}
return deferred.promise;
}
}