integrating google map api in angular 2 -
i trying use google map api find hospitals in location using library : https://developers.google.com/maps/documentation/javascript/places.
i succeded make request , display result in console want put result in variable display using ngfor in html page , found problem.enter image description here
here code:
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; (var = 0; < results.length; i++) { var place = results[i]; //this.hospsearch_res.push(place); /*console.log(results[i]); console.log(results[i].name);*/ } } }
this first project angular 2 , got stcuk problem, appreciated . thank you
just replace callback function arrow function bind 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) { ... } }
Comments
Post a Comment