setCenter not working in angular2-google-maps

后端 未结 3 1970
挽巷
挽巷 2021-01-31 11:53
import { GoogleMapsAPIWrapper } from \'@agm/core\';
import { Component, Input } from \'@angular/core\';

@Component({
  selector: \'core-map\',
  styleUrls: [ \'./map.co         


        
3条回答
  •  你的背包
    2021-01-31 12:16

    Finally got this working. Had to create a child component of agm-map and create an output that on load, grabs the native google maps api wrapper and passes into my parent map component. I wish they made it so you could just grab the gmaps api wrapper in the regular agm-map component. Works with panTo as well.

    PARENT COMPONENT MARKUP

    
      
      
    
    

    PARENT COMPONENT

    /**
     * Map Component
     * API Docs: https://angular-maps.com/docs/api/latest/ts/
     */
    import { GoogleMapsAPIWrapper } from '@agm/core';
    import { Component, Input } from '@angular/core';
    
    declare var google:any;
    
    @Component({
      selector: 'core-map',
      styleUrls: [ './map.component.scss' ],
      templateUrl: './map.component.html',
    })
    export class MapComponent {
      @Input() lat: number;
      @Input() lng: number;
      @Input() locations: {};
      map: any;
    
      constructor(
        public gMaps: GoogleMapsAPIWrapper,
      ) {}
    
      public loadAPIWrapper(map) {
        this.map = map;
      }
    
      public markerClicked = (markerObj) => {
        const position = new google.maps.LatLng(markerObj.latitude, markerObj.longitude);
        this.map.panTo(position);
      }
    }
    

    CHILD COMPONENT

    import { Component, EventEmitter, OnInit, Output } from '@angular/core';
    
    import { GoogleMapsAPIWrapper } from '@agm/core';
    
    @Component({
      selector: 'core-map-content',
      template: '',
    })
    export class MapContentComponent implements OnInit {
      @Output() onMapLoad: EventEmitter<{}> = new EventEmitter<{}>();
    
      constructor(public gMaps: GoogleMapsAPIWrapper) {}
    
      ngOnInit() {
        this.gMaps.getNativeMap().then((map) => {
          this.onMapLoad.emit(map);
        });
      }
    }
    

提交回复
热议问题