【问题标题】:setCenter not working in angular2-google-mapssetCenter 在 angular2-google-maps 中不起作用
【发布时间】:2020-12-03 02:10:42
【问题描述】:
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,结果相同。

【问题讨论】:

  • 您能否提供更多有关问题的详细信息?

标签: angular typescript angular2-google-maps


【解决方案1】:

终于搞定了。必须创建一个agm-map 的子组件并创建一个输出,在加载时获取本地 google maps api 包装器并传递到我的父地图组件。我希望他们做到了,这样您就可以在常规 agm-map 组件中获取 gmaps api 包装器。也适用于 panTo。

父组件标记

<agm-map [latitude]='lat' [longitude]='lng'
  [usePanning]='true'>
  <agm-marker *ngFor='let location of locations'
    [latitude]='location.latitude'
    [longitude]='location.longitude'
    [iconUrl]='location.icon'
    (markerClick)='markerClicked(location)'></agm-marker>
  <core-map-content (onMapLoad)='loadAPIWrapper($event)'></core-map-content>
</agm-map>

父组件

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

子组件

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

【讨论】:

  • 除了子组件之外没有别的办法了吗?
  • @f.khantsis 正确,这是我找到的唯一解决方案。这不是很优雅。
  • 我想补充一点,您不需要gMap 注入,因为实例将来自孩子。您也不需要将包装器添加到模块提供程序中,只需使用 this.map 实例。我不确定为什么默认情况下 agm 组件不公开包装器,但显然这是设计使然:“GoogleMapsAPIWraper 是在创建 agm-map 实例时创建的。这完全是故意的。我们为每个地图维护一个实例。如果你想要获取地图的实例,可以创建一个自定义组件,通过构造函数注入 GoogleMapsAPIWrapper。”
  • 简而言之,是的,这显然是唯一的方式和设计。太糟糕的 AGM 文档几乎不存在,这是一个很好的图书馆努力。
  • 哇,效果很好,谢谢分享。正如 cen 所提到的,很遗憾这没有得到更好的记录,我在这方面浪费了很多时间
【解决方案2】:

也许自从 Christopher 发布他的解决方案后就添加了它,但是 agm-map 有一个 mapReady 事件,它返回原始地图对象,您可以使用该对象访问 setCenter()。

修改您的原始代码:

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

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

<agm-map (mapReady)="mapReady($event)"></agm-map>

【讨论】:

  • 看起来好多了。将标记为正确,因为这看起来解决了对地图的访问问题。
  • 如果你这样做.map.panTo(position) 会很顺利:)
【解决方案3】:

google.maps.Map 可在 MapReady 事件中访问

<agm-map #geoMap [ngClass]="{'hide': !(showGeoMapDiagram$ | async)}"
    (window:resize)="onWindowResize()"    
    [latitude]="centerMapLat"
    [longitude]="centerMapLng"
    [mapTypeId]="mapTypeId"
    [mapTypeControl]="true"
    [zoom]="mapZoom"
    (mapReady)="onMapReady($event)"

>
</agm-map>

在代码“事件”中包含 google.maps.Map 的一个实例:

onMapReady(event: any)
  {
    this.diagramOverlay = new ShotDiagramOverlay();
    this.diagramOverlay.setMap(event);
    this.drawDiagramOnGeoMapSubscription = this.diagramOverlay.readyToDrawAction$.subscribe(() => this.drawDiagramOnGeoMap())
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 2017-05-20
    • 1970-01-01
    • 2015-02-10
    • 2013-11-04
    • 2017-12-21
    相关资源
    最近更新 更多