2017-08-31 3 views
0

Angular 2+アプリでGoogleマップを活用するコンポーネントがあります。コンポーネントのメソッドをトリガーするマーカーの情報ウィンドウへのリンクを得るための適切な方法はありますか?以下の(click)が問題点です。私がグローバルメソッドを作るのをやめたいならば(それを避けるために)onclickにスワップすることができます。Google Maps InfoWindow - (クリック)Angular 2関数をトリガーする

//Add directions if we have a current location 
let additionalContent = ""; 
if(this.positionMarker){ 
    additionalContent = `<br><a href='#' (click)='startNavigating(${marker.position.lat()}, ${marker.position.lng()})'>Directions</a>`; 
} 

let infoWindow = new google.maps.InfoWindow({ 
    content: content + additionalContent 
}); 

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

答えて

0

狂った角度のオーバーヘッドなしにこの作品を作成するためにいくつかの作品が見つかりました。情報ウィンドウが開いたらだったアンカーのIDにクリックリスナを追加し、アンカータグ

  • に(domready)に固有のIDを追加のみ1つの情報ウィンドウ(メンバー)を持っている
    1. :キーがしましたクリックされた以下の

    コード:

    //Add directions if we have a current location 
    let additionalContent = ""; 
    if (this.positionMarker) { 
        additionalContent = `<br><a href='#' id='marker_${markerId}'>Directions</a>`; 
    } 
    
    //Add the marker listener to open the infowindow 
    google.maps.event.addListener(marker, 'click',() => { 
        this.infoWindow.setContent(content + additionalContent); 
        this.infoWindow.open(this.map, marker); 
    
        //Once infow window is open, add a click listener based on the ID in the anchor tag 
        if (additionalContent) { 
        google.maps.event.addListenerOnce(this.infoWindow, 'domready',() => { 
         document.getElementById(`marker_${markerId}`).addEventListener('click',() => { 
         this.startNavigating(marker.position.lat(), marker.position.lng()); 
         }); 
        }); 
        } 
    }); 
    
    関連する問題