angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

getNativeMap() and getBounds() Promise never gets resolved neither rejected

Open giorgiofellipe opened this issue 9 years ago • 22 comments

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

giorgiofellipe avatar Mar 09 '17 14:03 giorgiofellipe

Please provide a plunkr

sebholstein avatar Mar 12 '17 11:03 sebholstein

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

giorgiofellipe avatar Mar 13 '17 13:03 giorgiofellipe

I have exactly the same issue

hesyar avatar Mar 17 '17 08:03 hesyar

I am also seeing the same thing with code similar to that provided in the code sample link on this thread . Is this functionality broken or is there a config file setting that we are all missing here ?

kleek430 avatar Mar 17 '17 13:03 kleek430

@SebastianM any news?

giorgiofellipe avatar Mar 28 '17 21:03 giorgiofellipe

@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);
    })
  }
}

zartdinov avatar Mar 30 '17 11:03 zartdinov

@zartdinov thanks, it worked!

But I didn't understand why it should be done this way, I should be able to access GoogleMapsAPIWrapper from outside, shouldn't I?

giorgiofellipe avatar Mar 30 '17 14:03 giorgiofellipe

@giorgiofellipe sorry for the delay. This is due to the fact that angular has a hierarchical dependency injection system and a new instance related to every map instance gets create for the map element and is therefor no accessible outside the map component.

I'm closing this as it works as expected

sebholstein avatar Mar 30 '17 19:03 sebholstein

Explains my issue as well. Calling this.wrapper.getNativeMap() never resolved because the wrapper was injected on a service; it needs to be injected on components that are children of the map.

@Injectable()
export class MapService {
  constructor(private wrapper: GoogleMapsAPIWrapper ...   < wrapper with no underlying map! 

ghost avatar Apr 09 '17 17:04 ghost

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);
	});
}

aikeda avatar Apr 26 '17 15:04 aikeda

aikeda, maybe you can post the whole code ?

kapocius avatar Apr 28 '17 20:04 kapocius

@SebastianM why does getBounds() still return undefined?? Everything else seems to work, but getBounds is always undefined.

franknwankwo avatar May 01 '17 17:05 franknwankwo

@aikeda Doesn't seem to work for me. In the template <sebm-google-map #AgmMap ...

In the component @ViewChild('AgmMap') agmMap; ngAfterViewInit() { console.log(this.agmMap); }

The object logged is of type SebmGoogleMap, it doesn't have any mapReady event to subscribe to. Can you help out or post your code?

aamirkhan-91 avatar May 10 '17 10:05 aamirkhan-91

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

gpolyn avatar May 11 '17 14:05 gpolyn

@aamirkhan-91 @SebastianM I tried something similar for AgmCircle

I had <agm-circlce #circle ...

and tried referring it as @ViewChild('circle') AgmCircle;

I only get it as a ElementRef object and not as AgmCircle. I intend to use the getBounds() onNgInit() but it doesn't seem to work.

http://plnkr.co/edit/nG6lUsm1KLbQRv3SRdgW?p=preview

pravinkumars avatar Jun 15 '17 06:06 pravinkumars

Did you find any solution? Every ViewChild has the correct type except AgmCircle which is an ElementRef

The documentation of this project is kind of ambiguous because it's just a "copy" of the code :(

acuntex avatar Jun 28 '17 11:06 acuntex

try with @ViewChild(AgmCircle) myCircle; It worked.

The selector was referenced wrongly in my earlier work

pravinkumars avatar Jul 01 '17 17:07 pravinkumars

@pravinkumars @acuntex @ViewChild('circle', {read: AgmCircle}) circle: AgmCircle; This allows you to get control of a specific circle with a named template variable like <agm-circle #circle > </agm-circle>

SterlingMcCall avatar Aug 11 '17 19:08 SterlingMcCall

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.

adrianhara avatar Sep 08 '17 07:09 adrianhara

@aikeda ,

sivakavi-manureva avatar Jan 09 '18 10:01 sivakavi-manureva