Instead of
$('#modal-location').on('shown.bs.modal', () => {
initMap();
});
I used this.
$(document).on('shown.bs.modal', '#modal-location', () => {
initMap();
});
and it works fine.
because it ensures initMap()) executes after the modal is actually visible in the DOM.