2016-09-04 22 views
0

Google Maps SDKを使用してIonic 2アプリを構築しています。関数の結果をプロパティに渡す

マップ上にマーカーを追加できるようにしたいと考えています。

ボタンクリックでマーカーを追加私のコードは次のようになります。

addMarker(){ 

    let marker = new google.maps.Marker({ 
     map: this.map, 
     animation: google.maps.Animation.DROP, 
     position: this.map.getCenter() 
    }); 

    let content = "<h4>Information!</h4>";   

    this.addInfoWindow(marker, content); 

} 

位置:this.map.getCenter()は、ピンは常に地図の中心に追加できます。

addMarker関数に挿入する

addNewPlace(){ 
    let newPlace = new google.maps.event.addListener(this.map, 'click', (event) => latLng); 
} 

及びIは、(緯度経度)上記からの結果が欲しい:

次の関数は、地図をクリックした場所の緯度/経度を返すべき

addMarker(){ 

    let marker = new google.maps.Marker({ 
     map: this.map, 
     animation: google.maps.Animation.DROP, 
     position: newPlace 
    }); 

    let content = "<h4>Information!</h4>";   

    this.addInfoWindow(marker, content); 

} 

どうすればいいですか? 今のところ、ブラウザは単に私のaddNewPlace機能を無視します(何も起こらず、コンソールにエラーはありません)。

答えて

2

マップを作成する初期化関数で、マップ用のクリックリスナーを追加できます。 addNewPlace()関数を作成する必要はありません。 google.maps.event.addListener()のコンストラクタはありません。新しい演算子の必要はありません。クリックイベントのため

https://developers.google.com/maps/documentation/javascript/reference#event

コールバック関数は、マップの作成が

を追加した後

https://developers.google.com/maps/documentation/javascript/reference#MouseEvent

だから、あなたの初期化関数では機能

addMarker(newPlace) { 

    let marker = new google.maps.Marker({ 
     map: this.map, 
     animation: google.maps.Animation.DROP, 
     position: newPlace 
    }); 

    let content = "<h4>Information!</h4>";   

    this.addInfoWindow(marker, content); 
} 

を作成することができMouseEventオブジェクトを返します。

google.maps.event.addListener(this.map, 'click', (event) => { 
    addMarker(event.latLng); 
}); 
関連する問題