I'm working on an Ionic 3 app which uses Ionic Native Google Maps plugin. I'm trying to use node pool with the Google Maps instance to create a marker pool.
My problem is that inside the new Promise in the createPool function, this.map is undefined, even though it has been created by the loadMap() function. I read this awesome post about promises but I can't figure out how that applies to my case. I would be very grateful if anyone could help me.
ngAfterViewInit() {
this.platform.ready()
.then(_ => {
return this.loadMap()
})
.then(_ => {
this.markerpool = this.createPool();
this.markerpool.on('factoryCreateError', err => console.log(err));
})
}
createPool() {
var factory = {
create: function () {
return new Promise((resolve, reject) => {
this.map.addMarker({
icon: 'assets/icon/sun.png',
visible: false
})
.then(marker => {
// icon anchor set to the center of the icon
marker.setIconAnchor(42, 37);
resolve(marker);
})
})
},
destroy: function (marker: Marker) {
return new Promise((resolve, reject) => {
marker.remove();
resolve();
})
}
}
var opts = {
max: 60, // maximum size of the pool
min: 20 // minimum size of the pool
}
return GenericPool.createPool(factory, opts);
}
loadMap() {
this.mapElement = document.getElementById('map');
let mapOptions: GoogleMapOptions = {
camera: {
target: {
lat: 40.9221968,
lng: 14.7907662
},
zoom: maxzoom,
tilt: 30
},
preferences: {
zoom: {
minZoom: minzoom,
maxZoom: maxzoom
}
}
};
this.map = this.googleMaps.create(this.mapElement, mapOptions);
this.zoomLevel = this.getZoomLevel(maxzoom);
return this.map.one(GoogleMapsEvent.MAP_READY);
}