Dynamically add google maps div into DOM

I’m wanting to add the Google Maps API div, <div id="map"></div> within a card that’s dynamically added to the DOM, after a user has input a search. I’m able to append it the main div, as a test, but not within the card one I’m appending after the the card is inserted into the DOM.

Code is below.

Any help would be great. Thank you

const APIURL = 'https://restcountries.eu/rest/v2/name/'
const GOOGLE_MAPS_API = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyDhlU1KMTlTh4C__bTJBxbVA-s7wvQbO9E&callback=initMap'
const main = document.getElementById('main')
const form = document.getElementById('form')
const search = document.getElementById('search')

async function getCountryData(name) {
    try {
        const { data } = await axios.get(APIURL + name)

        data.forEach(res => {
            const countryData = res

            addGMapsEl()
            createCountryCard(countryData)
            getLatLngPos(countryData)
        } )
        } catch (err) {
        if(err.response.status == 404) {
            createErrorCard('No countries found')
            setTimeout(() => { 
                main.innerHTML = ''}
                , 1500);
        }
    }
}

// Google Map API 
function addGMapsEl() {
const script = document.createElement('script');
script.src = GOOGLE_MAPS_API;
script.defer = true;

document.head.appendChild(script);

const mapDiv = document.createElement('div')

      mapDiv.id = 'map'
      main.appendChild(mapDiv)
}

let map;

function initMap() {
  
  map = new google.maps.Map(document.getElementById("map"), {
    center: { lat: 51, lng: 9},
    zoom: 7
  });
}

function createCountryCard(country) {
    const cardHTML = `
    <div class="content-container">
        <div class="card">
         <div class="wrapper">
             <div class="card-title">
              <h2>${country.name}</h2>
              <h4>Capital: ${country.capital}</h4>
              <h5>Population: ${country.population.toLocaleString('en')}</h5>
        </div>
        <div class="card-image">
          <img
            src="${country.flag}"
            alt="${country.name +'-flag'}"
          />
        </div>
      </div>
      <div class="wrapper">
        <div class="map-content">
        </div>
        <div class="card-content">
          <ul class="card-list">
            <li><strong>Region:</strong> ${country.region}</li>
            <li><strong>Subregion:</strong> ${country.subregion}</li>
            <li><strong>Currency:</strong> ${country.currencies[0].name}<span> ${country.currencies[0].symbol}</span></li>
            <li><strong>Spoken Language:</strong> ${country.languages[0].name}</li>
            <li><strong>Timezone:</strong> ${country.timezones}</li>
          </ul>
        </div>
      </div>
    </div>
  </div>
  `
  main.innerHTML += cardHTML
}

// Creates error card after no results found
function createErrorCard(msg) {
    const cardHTML = `
    <div class="card">
        <h1>${msg}</h1>
    </div>
    `
    main.innerHTML = cardHTML
}

// Clears the DOM on search 
function clearDOM() {
  main.innerHTML = ''
}



// Search Input
form.addEventListener('submit', (e) => {
    e.preventDefault()
 
    clearDOM()

    const countryName = search.value
    if(countryName) {
        getCountryData(countryName)

        search.value = ''
    }
})
<body>
        <div class="search-container">
          <form id="form" class="form">
            <input type="text" id="search" placeholder="Search for country..." />
          </form>
        </div>
        <main id="main"></main>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>
        <script src="script.js"></script>
      </body>

94 thoughts on “Dynamically add google maps div into DOM”

  1. i need a loan with bad crediti need a loan fast, need a loan of 5000 with bad credit. i need a loan desperately today need loan now need a loan been refused everywhere i need loan fast, cash advance loans what do i need, cash advance, cash advance loans, cash advance loans for 1500. Money management study of those commerce, financial institution .

    Reply