I use Places Service to load locations that are around the default location. The problem is that when I click the marker, it opens an infowindow but doesn’t close other open infowindows. How to open an infowindow and close other open infowindows?
Initial marker

nearbySearch: I want when I click the marker, other open infowindows will be closed

below are the codes that I have created
<script>
function initMap() {
const defaultLocation = { lat: {{ $property->latitude }}, lng: {{ $property->longitude }} };
const map = new google.maps.Map(document.getElementById('property-full-address-map'), {
center: defaultLocation,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
streetViewControl: false,
gestureHandling: "greedy",
styles: [ { "elementType": "geometry", "stylers": [ { "color": "#f5f5f5" } ] }, { "elementType": "labels.icon", "stylers": [ { "visibility": "off" } ] }, { "elementType": "labels.text.fill", "stylers": [ { "color": "#616161" } ] }, { "elementType": "labels.text.stroke", "stylers": [ { "color": "#f5f5f5" } ] }, { "featureType": "administrative.land_parcel", "elementType": "labels.text.fill", "stylers": [ { "color": "#bdbdbd" } ] }, { "featureType": "poi", "elementType": "geometry", "stylers": [ { "color": "#eeeeee" } ] }, { "featureType": "poi", "elementType": "labels.text.fill", "stylers": [ { "color": "#757575" } ] }, { "featureType": "poi.park", "elementType": "geometry", "stylers": [ { "color": "#e5e5e5" } ] }, { "featureType": "poi.park", "elementType": "labels.text.fill", "stylers": [ { "color": "#9e9e9e" } ] }, { "featureType": "road", "elementType": "geometry", "stylers": [ { "color": "#ffffff" } ] }, { "featureType": "road.arterial", "elementType": "labels.text.fill", "stylers": [ { "color": "#757575" } ] }, { "featureType": "road.highway", "elementType": "geometry", "stylers": [ { "color": "#dadada" } ] }, { "featureType": "road.highway", "elementType": "labels.text.fill", "stylers": [ { "color": "#616161" } ] }, { "featureType": "road.local", "elementType": "labels.text.fill", "stylers": [ { "color": "#9e9e9e" } ] }, { "featureType": "transit.line", "elementType": "geometry", "stylers": [ { "color": "#e5e5e5" } ] }, { "featureType": "transit.station", "elementType": "geometry", "stylers": [ { "color": "#eeeeee" } ] }, { "featureType": "water", "elementType": "geometry", "stylers": [ { "color": "#91D5E4" } ] }, { "featureType": "water", "elementType": "labels.text.fill", "stylers": [ { "color": "#9e9e9e" } ] } ]
});
var marker = new google.maps.Marker({
position: defaultLocation,
map,
title: "Property Location",
});
marker.setMap(map);
}
$(document).ready(function() {
$('.nearby-search-btn').click(function() {
const type = $(this).data('type');
const defaultLocation = { lat: {{ $property->latitude }}, lng: {{ $property->longitude }} };
const map = new google.maps.Map(document.getElementById('property-full-address-map'), {
center: defaultLocation,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
streetViewControl: false,
gestureHandling: "greedy",
styles: [ { "elementType": "geometry", "stylers": [ { "color": "#f5f5f5" } ] }, { "elementType": "labels.icon", "stylers": [ { "visibility": "off" } ] }, { "elementType": "labels.text.fill", "stylers": [ { "color": "#616161" } ] }, { "elementType": "labels.text.stroke", "stylers": [ { "color": "#f5f5f5" } ] }, { "featureType": "administrative.land_parcel", "elementType": "labels.text.fill", "stylers": [ { "color": "#bdbdbd" } ] }, { "featureType": "poi", "elementType": "geometry", "stylers": [ { "color": "#eeeeee" } ] }, { "featureType": "poi", "elementType": "labels.text.fill", "stylers": [ { "color": "#757575" } ] }, { "featureType": "poi.park", "elementType": "geometry", "stylers": [ { "color": "#e5e5e5" } ] }, { "featureType": "poi.park", "elementType": "labels.text.fill", "stylers": [ { "color": "#9e9e9e" } ] }, { "featureType": "road", "elementType": "geometry", "stylers": [ { "color": "#ffffff" } ] }, { "featureType": "road.arterial", "elementType": "labels.text.fill", "stylers": [ { "color": "#757575" } ] }, { "featureType": "road.highway", "elementType": "geometry", "stylers": [ { "color": "#dadada" } ] }, { "featureType": "road.highway", "elementType": "labels.text.fill", "stylers": [ { "color": "#616161" } ] }, { "featureType": "road.local", "elementType": "labels.text.fill", "stylers": [ { "color": "#9e9e9e" } ] }, { "featureType": "transit.line", "elementType": "geometry", "stylers": [ { "color": "#e5e5e5" } ] }, { "featureType": "transit.station", "elementType": "geometry", "stylers": [ { "color": "#eeeeee" } ] }, { "featureType": "water", "elementType": "geometry", "stylers": [ { "color": "#91D5E4" } ] }, { "featureType": "water", "elementType": "labels.text.fill", "stylers": [ { "color": "#9e9e9e" } ] } ]
});
var marker = new google.maps.Marker({
position: defaultLocation,
map,
icon: {
url: "{{ asset('front/img/marker.png') }}",
scaledSize: new google.maps.Size(40, 40)
},
title: "Property Location",
});
var request = {
location: defaultLocation,
radius: '500',
type: [type]
};
var service = new google.maps.places.PlacesService(map);
service.nearbySearch(request, callback);
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
var place = results[i];
createMarker(results[i]);
}
}
}
function createMarker(place) {
var neighborhoodMarker = new google.maps.Marker({
map: map,
position: place.geometry.location,
icon: {
url: place.icon,
scaledSize: new google.maps.Size(25, 25)
},
label: ''
});
var ratingCount = place.rating;
var rating = '';
//add icon for rating count
if (ratingCount >= 4.5) {
rating = '<i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i>';
} else if (ratingCount >= 4) {
rating = '<i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i>';
} else if (ratingCount >= 3.5) {
rating = '<i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning-half"></i>';
} else if (ratingCount >= 3) {
rating = '<i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i>';
} else if (ratingCount >= 2.5) {
rating = '<i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning-half"></i>';
} else if (ratingCount >= 2) {
rating = '<i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning"></i>';
} else if (ratingCount >= 1.5) {
rating = '<i class="fa-solid fa-star text-warning"></i><i class="fa-solid fa-star text-warning-half"></i>';
} else if (ratingCount >= 1) {
rating = '<i class="fa-solid fa-star text-warning"></i>';
} else {
rating = '<i class="fa-solid fa-star-half"></i>';
}
var contentString = `
<div class="card border-0 min-width-200px">
<div class="card-body px-0">
<span class="fw-bold text-brand-secondary-color fs-5">` + place.name + `</span>
<p class="text-brand-secondary-color mt-2">` + place.vicinity + `</p>
<div class="d-flex justify-content-between align-items-center">
<span class="small text-brand-secondary-color">` + rating + `</span>
<span class="small text-brand-secondary-color">` + place.user_ratings_total + ` Reviews</span>
</div>
</div>
</div>
`;
var neighborhoodInfoWindow = new google.maps.InfoWindow({
content: contentString
});
neighborhoodMarker.addListener('click', function() {
neighborhoodInfoWindow.open(map, neighborhoodMarker);
});
google.maps.event.addListener(map, 'click', function() {
neighborhoodInfoWindow.close();
});
}
});
});
</script>