2017-04-14 125 views
0

我想使用谷歌地圖API找到醫院在某些位置使用此庫:https://developers.google.com/maps/documentation/javascript/places谷歌地圖api角度2

我成功地在控制檯中發出請求並顯示結果,但我想把結果放在變量中,以便在html頁面中使用ngFor來顯示它,這就是我發現這個問題的地方。 enter image description here

這裏是我的代碼:

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,任何幫助將不勝感激。 謝謝

+0

它說這是不確定的.. – Robert

+0

是的,這是錯誤。卡爾波斯響應解決te問題,無論如何謝謝 – Riadh

+0

嗨,我是新的角2。你可以請幫助,如何使angular2服務谷歌地圖API –

回答

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