我正在使用AGM谷歌地图组件。我用*ngFor在模板中添加了标记。
import { Component, NgModule} from '@angular/core';
import { BrowserModule} from '@angular/platform-browser';
import { AgmCoreModule} from '@agm/core';
import { AgmJsMarkerClustererModule, ClusterManager } from '@agm/js-marker-clusterer;
@Component({
selector: 'my-app',
styles: [`
.agm-map-container {
height: 300px;
}
`],
template: `
<agm-map #gm style="height: 300px" [latitude]="52.692868" [longitude]="7.834982">
<agm-marker *ngFor="let m of markers; let i = index" (mouseOver)="onMouseOver(infoWindow,gm)" [latitude]="m.latitude" [longitude]="m.longitude">
<agm-info-window [disableAutoPan]="false" #infoWindow>
Test
</agm-info-window>
</agm-marker>
</agm-map>
<button type="button" (click)="onFocusMarker1($event)">Focus marker 1</button>
<button type="button" (click)="onFocusMarker2($event)">Focus marker 2</button>
`})
export class AppComponent {
markers: Array<any>
constructor(){
this.markers=[{latitude:52.692868, longitude:7.834982},{latitude:52.992868, longitude:7.034982}]
}
onFocusMarker1(event){
event.preventDefault();
alert("How to take reference of first marker, centrer the map on it and open the info window?");
}
onFocusMarker2(event){
event.preventDefault();
alert("How to take reference of second marker, centrer the map on it and open the info window?");
}
onMouseOver(infoWindow, gm) {
if (gm.lastOpen != null) {
gm.lastOpen.close();
}
gm.lastOpen = infoWindow;
infoWindow.open();
}
}现在,我需要在地图外部按一个按钮,并在标记上居中,然后打开它的InfoWindow,但是我找不到一种方法来获取标记引用来实现这一点。
我创建了一个Plunkr 这里
发布于 2018-11-04 13:32:00
我也曾与同样的问题作过斗争。虽然我无法获得对标记本身的引用,但我最终采用了声明式的方式,并使用了AgmInfoWindow.isOpen 看这里。
<agm-marker *ngFor="let m of markers; let i = index" (mouseOver)="onMouseOver(infoWindow,gm)" [latitude]="m.latitude" [longitude]="m.longitude">
<agm-info-window [disableAutoPan]="false" [isOpen]="m.isOpen" #infoWindow>
Test
</agm-info-window>
</agm-marker>如果现在单击地图外的按钮,则只需将所选择的标记设置为
this.markers[0].isOpen = true; // I strongly assume you have a more sophisticated way of selecting your marker窗户就会打开!
https://stackoverflow.com/questions/46930764
复制相似问题