我可以将Angular2(点击)添加到Google地图标记infoWindow吗?

时间:2017-04-27 13:47:50

标签: google-maps angular

我已经在Angular2中实现了Google地图,而没有使用任何可用的npm库,并且需要在infoWindow中添加一个按钮来触发Angular函数。可以这样做吗???。

到目前为止,我的最大努力不起作用,airport.name显示但是(click)=“setAirport('出发',机场)”没有。我认为它没有以任何方式连接到Angular。

当用户点击激活标记时,我的组件具有以下功能。信息窗口。

showAirport(idx,airport){     const markerInfo =     <h4>${airport.name}</h4><div class='row' style='margin:0; margin-top:20px'> <div class="col-xs-6"> <a class="btn btn-primary btn-block" (click)="setAirport('departure',airport)">Set as Departure</a> </div> <div class="col-xs-6"> <a class="btn btn-primary btn-block" (click)="setAirport('arrival',airport)">Set as Arrival</a> </div> </div>
;

this.setCurrentAirport(idx);
this.infoWindow.setContent(markerInfo);
this.infoWindow.open(this.map, this.markers[idx]);

}

以下是组件ts文件的更多内容。

import { Component, ViewChild, ElementRef, OnInit } from '@angular/core';
import { Response } from '@angular/http';

declare var google;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
    @ViewChild('map') mapElement: ElementRef;

    map: any;
    infoWindow = new google.maps.InfoWindow();

    private allItems: any[];

    lat = 51.5287718;
    lng = -0.1;

    airports = [];
    markers = [];
    flights = [];
    polylines = [];

    bounds = {
      w: 0,
      e: 0,
      n: 0,
      s: 0
    };

    loadAirports(apiBounds) {
        this.airportsService.byBounds(apiBounds)
        .subscribe(
          (airportData: any[]) => {
              for (let i = 0; i < airportData['data'].length; i++) {
                  const tmpAirport = {
                      id: i,
                      airportID: airportData['data'][i].id,
                      name: airportData['data'][i].airportName,
                      lat: airportData['data'][i].lat,
                      lng: airportData['data'][i].lng
                  };
                  this.airports.push(tmpAirport);
                  this.addMarker(tmpAirport, tmpAirport['name'], i);
              }
          },
          (error: Response) => {
            const errorData = error.json();
          }
        );
    };

  showAirport(idx, airport) {
    console.log(airport);
    // this is where I want to be able to include the buttons 
    const markerInfo = `<h4>${airport.name}</h4>`;
    this.infoWindow.setContent(markerInfo);
    this.infoWindow.open(this.map, this.markers[idx]);
  }

  loadMap() {
    const latLng = new google.maps.LatLng(51, -0.5);
    const mapOptions = {
      center: latLng,
      scrollwheel: false,
      zoom: 9,
      minZoom: 6,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);

    new google.maps.event.addListenerOnce(this.map, 'bounds_changed', () => {
        this.loadAirports(this.bounds);
    });
  }

  addMarker(airport, info, markerIndex) {
    const position = new google.maps.LatLng(airport['lat'], airport['lng']);
    const marker = new google.maps.Marker({
      map: this.map,
      position: position,
      markerIndex: markerIndex
    });
    const markerInfo = '<b>' + airport.name + '</b>';
    this.addInfoWindow(marker, markerInfo, markerIndex);
    this.markers.push(marker);
  }

  addInfoWindow(marker, content, markerIndex) {
    google.maps.event.addListener(marker, 'click', () => {
      this.infoWindow.setContent(content);
      this.infoWindow.open(this.map, marker);
    });
  }

  constructor() {}

  ngOnInit() {
    this.loadMap();
  }

}

2 个答案:

答案 0 :(得分:1)

不是最漂亮的解决方案,但你总是可以将clases / id分配给InfoWindow中的按钮,然后绑定到它们:

showAirport(idx, airport) {
  console.log(airport);
  // this is where I want to be able to include the buttons 
  const markerInfo = `
    <h4>${airport.name}</h4>
    <a class="btn btn-primary btn-block" id="departure-button">Set as Departure</a>
  `;
  this.infoWindow.setContent(markerInfo);
  this.infoWindow.open(this.map, this.markers[idx]);

  const departureButton = this.mapElement.nativeElement.getElementById('departure-button');
  departureButton.onclick = this.setAirport('departure', airport);

  google.maps.event.addListener(this.infoWindow, 'closeclick', () => {
    departureButton.onclick = null;
  });
});

答案 1 :(得分:0)

只需将其分配给它并使用它,

  addInfoWindow(marker, content, markerIndex) {
    let that = this;
    google.maps.event.addListener(marker, 'click', () => {
      that.infoWindow.setContent(content);
      that.infoWindow.open(this.map, marker);
    });
  }

疯狂吧;)。 this的变量范围不是您需要的this,因此这不是infoWindow,而是将this分配给that现在我们infowWindow }}