setCenter无法在angular2-google-maps中工作

前端之家收集整理的这篇文章主要介绍了setCenter无法在angular2-google-maps中工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import { GoogleMapsAPIWrapper } from '@agm/core';
import { Component,Input } from '@angular/core';

@Component({
  selector: 'core-map',styleUrls: [ './map.component.scss' ],templateUrl: './map.component.html',})
export class MapComponent {
  constructor(
    public gMaps: GoogleMapsAPIWrapper
  ) {}

  public markerClicked = (markerObj) => {
    this.gMaps.setCenter({ lat: markerObj.latitude,lng: markerObj.longitude });
    console.log('clicked',markerObj,{ lat: markerObj.latitude,lng: markerObj.longitude });
  }
}
console output: Object {lat: 42.31277,lng: -91.24892}

也试过panTo同样的结果.

也许它是在Christopher发布他的解决方案后添加的,但是agm-map有一个mapReady事件,它返回可用于访问setCenter()的原始地图对象.

修改原始代码

import { Component,})
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 });
  }
}

然后将以下内容添加到agm-map

<agm-map (mapReady)="mapReady($event)"></agm-map>
原文链接:https://www.f2er.com/angularjs/143892.html

猜你在找的Angularjs相关文章