2016-09-04 4 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()이 핀하게 항상지도의 중앙에 추가합니다.

다음 함수는지도에서 클릭 된 장소의 위도/경도를 반환해야합니다 :

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

와 나는 addMarker 기능에 삽입 할 위의 결과 (latLng를를) 원하는 :

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); 
}); 
관련 문제