2017-04-14 1 views
0

Google지도 API를 사용하여이 라이브러리를 사용하여 특정 위치의 병원을 찾으려고합니다 : https://developers.google.com/maps/documentation/javascript/places.각도 2의 google map api 통합

요청을 콘솔에 표시하고 결과를 표시하려고했지만 결과를 변수에 넣어 html 페이지의 ngFor를 사용하여 표시하고 싶습니다.이 위치에서이 문제를 발견했습니다.

hospSearch_res:any[] = []; 
searchHospitals(){ 
console.log('Search works'); 
console.log(this.hosp_lat); 
console.log(this.hosp_lng); 
console.log(this.hosp_rad); 

var search_loc = new google.maps.LatLng(this.lat,this.lng); 

var request = { 
    location: search_loc, 
    radius: '1000', 
    types: ['hospital'] 
}; 

var service = new google.maps.places.PlacesService(document.getElementById('test')); 
service.nearbySearch(request, this.callback); 

} 

callback(results, status) { 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
    // this.hospSearch_res = results; 
    //let res = JSON.stringify(results); 
    console.log(results); 
    this.hospSearch_res = results; 
    for (var i = 0; i < results.length; i++) { 
     var place = results[i]; 
     //this.hospSearch_res.push(place); 
     /*console.log(results[i]); 
     console.log(results[i].name);*/ 

    } 

    } 
} 

이 각 2 내 첫 번째 프로젝트이며, 나는이 문제에 대한 stcuk있어, 어떤 도움이 많이 주시면 감사하겠습니다 : enter image description here

여기 내 코드입니다. 당신이

+0

그것은이 정의되지 않은 말한다 .. – Robert

+0

예 그 오류가 있었다. Karbos 응답 문제가 해결되었습니다. 어쨌든 고맙습니다. – Riadh

+0

안녕하세요, 각도가 2입니다. Google지도 API에 대한 angular2에서 서비스를 만드는 방법을 알려주세요. –

답변

1

그냥 this 바인딩 화살표 기능에 의해 콜백 함수를 대체 감사합니다

var service = new 
google.maps.places.PlacesService(document.getElementById('test')); 
service.nearbySearch(request, this.callback); 

} 

callback = (results, status) => { //Arrow Function Here ! 
    if (status == google.maps.places.PlacesServiceStatus.OK) { 
    ... 
    } 
} 
+0

빠른 응답을 주셔서 감사합니다. 분명히 나는 ​​앵귤러 2에서 아직도 배울 점이 많다. – Riadh