Chr*_*all 15 typescript angular2-google-maps angular
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同样的结果.
Chr*_*all 26
终于搞定了这个.必须创建一个子组件agm-map并创建一个加载输出,抓取本机谷歌地图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);
    });
  }
}
Bri*_*ton 18
也许它是在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>
| 归档时间: | 
 | 
| 查看次数: | 12521 次 | 
| 最近记录: |