setCenter not working in angular2-google-maps

后端 未结 3 1923
挽巷
挽巷 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:02

    Perhaps it's been added since Christopher posted his solution, but agm-map has a mapReady event that returns the raw map object that you can use to access setCenter().

    Modification of your original code:

    import { Component, Input } from '@angular/core';
    
    @Component({
      selector: 'core-map',
      styleUrls: [ './map.component.scss' ],
      templateUrl: './map.component.html',
    })
    export class MapComponent {
      protected map: any;
    
      constructor() {}
    
      protected mapReady(map) {
        this.map = map;
      }
    
      public markerClicked = (markerObj) => {
        if (this.map)
          this.map.setCenter({ lat: markerObj.latitude, lng: markerObj.longitude });
        console.log('clicked', markerObj, { lat: markerObj.latitude, lng: markerObj.longitude });
      }
    }
    

    Then add the following to agm-map

    
    

提交回复
热议问题