终于得到了这个工作。必须创建一个子组件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);
});
}
}