angular-google-maps: getNativeMap() and getBounds() Promise never gets resolved neither rejected

I’m trying to get native map instance to do fitBounds but it’s Promise never gets resolved neither rejected. I’ve already tried to do this in a component inside sebm-google-map but it also didn’t work.

    <sebm-google-map>
      <map-content></map-content>
    </sebm-google-map>

Plunker: http://plnkr.co/edit/VKqwVSUlzFJjHPX7cQm4?p=preview

v0.17.0

@angular/cli: 1.0.0-beta.32.3
node: 6.9.5
os: darwin x64
@angular/common: 2.4.8
@angular/compiler: 2.4.8
@angular/core: 2.4.8
@angular/forms: 2.4.8
@angular/http: 2.4.8
@angular/platform-browser: 2.4.8
@angular/platform-browser-dynamic: 2.4.8
@angular/platform-server: error
@angular/router: 3.4.8
@angular/cli: 1.0.0-beta.32.3
@angular/compiler-cli: 2.4.8

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 22 (2 by maintainers)

Most upvoted comments

Hi all! You do not need a child directive to obtain your native map instance. You can get it subscribing for mapReady events on AgmMap object.

@ViewChild(AgmMap) agmMap;
ngAfterViewInit(){
	this.agmMap.mapReady.subscribe(map => {
		var trafficLayer = new google.maps.TrafficLayer();
		trafficLayer.setMap(map);
	});
}

In case anyone still has problems, you don’t need even a @ViewChild, as the mapReady is an event on the map component, so in your template just do <agm-map (mapReady)="onMapReady($event)"... and in your component define the onMapReady(map) method.

@giorgiofellipe It works!

I did like in comment (#715) (there are outdated, not working code example). Sorry, I don’t have time to full plunkr example, may be later. Try to use child directive.

@Directive({
  selector: 'my-custom-extension'
})
export class MyCustomExtension implements AfterViewInit{
  constructor(private _wrapper: GoogleMapsAPIWrapper) {
    console.log('constructor')
  }

  ngAfterViewInit() {
    console.log('ngAfterViewInit');
    this._wrapper.getNativeMap().then((m) => {
      console.log('native map',m);
    }, err=>{
      console.log('error',err);
    })
  }
}

@aamirkhan-91 Confirm I get the same result as you

aikeda, maybe you can post the whole code ?