I am new to Angular 5 and I am attempting to display nearby restaurant results using Google Maps in Angular 5. However, I am unsure of where to create the array in TypeScript and how to pass this array into the HTML when searching for "restaurant". I have attempted to create a JavaScript function to capture the value and pass it to the component's HTML, but I have not been successful.
Can anyone assist me with this?
ngOnInit() {
var mapProp = {
center: new google.maps.LatLng(3.147609, 101.698625),
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
this.map = new google.maps.Map(this.gmapElement.nativeElement,mapProp);
var map = this.map;
var infoWindow = new google.maps.InfoWindow;
var input = <HTMLInputElement>document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.addListener('click',function(event) {
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
});
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
var markers = [];
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
console.log(place.name);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
}
Ben