首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参考AGM谷歌地图外部的标记

参考AGM谷歌地图外部的标记
EN

Stack Overflow用户
提问于 2017-10-25 11:06:01
回答 1查看 1.1K关注 0票数 4

我正在使用AGM谷歌地图组件。我用*ngFor在模板中添加了标记。

代码语言:javascript
复制
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 这里

EN

回答 1

Stack Overflow用户

发布于 2018-11-04 13:32:00

我也曾与同样的问题作过斗争。虽然我无法获得对标记本身的引用,但我最终采用了声明式的方式,并使用了AgmInfoWindow.isOpen 看这里

代码语言:javascript
复制
<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>

如果现在单击地图外的按钮,则只需将所选择的标记设置为

代码语言:javascript
复制
this.markers[0].isOpen = true; // I strongly assume you have a more sophisticated way of selecting your marker

窗户就会打开!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46930764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档