Search code examples
google-maps-api-3infowindow

How to display a clock on a google maps infowindow


I'm trying to display a clock on my infowindow. The clock runs fine when I it log in the webconsole, but whenever I try to display it on my infowindow, it won't work and it will display "undefined".

I tried adding the GetClock() function which returns the time like this:

        var MiamiContent =
            '<h3> Miami </h3><br ><h5>'+ setInterval(GetClock, 1000) +' </h5>' 

        var MiamiInfoCard = new google.maps.InfoWindow
            ({
                content: MiamiContent
            });

This is the function that returns the time. It works fine.

    tday = new Array("Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat");
    tmonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
    function GetClock() {
        var d = new Date();
        var nday = d.getDay(), nmonth = d.getMonth(), ndate = d.getDate(), nyear = d.getYear(), nhour = d.getHours(), nmin = d.getMinutes(), nsec = d.getSeconds(), ap;
        if (nhour == 0) { ap = " AM"; nhour = 12; }
        else if (nhour < 12) { ap = " AM"; }
        else if (nhour == 12) { ap = " PM"; }
        else if (nhour > 12) { ap = " PM"; nhour -= 12; }

        if (nyear < 1000) nyear += 1900;
        if (nmin <= 9) nmin = "0" + nmin;
        if (nsec <= 9) nsec = "0" + nsec;

        console.log(tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear + " " + nhour + ":" + nmin + ":" + nsec + ap + "")
    }

    window.onload = function () {
        GetClock();
        setInterval(GetClock, 1000);
    }

I'm assuming something is wrong with the way I call the function within the MiamiContent variable since the function does work in my console. Or it's because I log it in my function and the infowindow doesn't know how to "log" things. Help is very much appreciated


Solution

  • If you want the GetClock function to display in the DOM of the InfoWindow, you need to write the code to do that. For example:

    var MiamiContent =
      '<h3> Miami </h3><br ><h5><span id="clock"></span></h5>'
    
    var MiamiInfoCard = new google.maps.InfoWindow({
      content: MiamiContent
    });
    

    Then in GetClock:

      tday = new Array("Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat");
      tmonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
    
      function GetClock() {
        var d = new Date();
        var nday = d.getDay(),
          nmonth = d.getMonth(),
          ndate = d.getDate(),
          nyear = d.getYear(),
          nhour = d.getHours(),
          nmin = d.getMinutes(),
          nsec = d.getSeconds(),
          ap;
        if (nhour == 0) {
          ap = " AM";
          nhour = 12;
        } else if (nhour < 12) {
          ap = " AM";
        } else if (nhour == 12) {
          ap = " PM";
        } else if (nhour > 12) {
          ap = " PM";
          nhour -= 12;
        }
    
        if (nyear < 1000) nyear += 1900;
        if (nmin <= 9) nmin = "0" + nmin;
        if (nsec <= 9) nsec = "0" + nsec;
    
        console.log(tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear + " " + nhour + ":" + nmin + ":" + nsec + ap + "")
        var clockSpan = document.getElementById('clock');
        if (!!clockSpan) {
          clockSpan.textContent = tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear + " " + nhour + ":" + nmin + ":" + nsec + ap + "";
        }
      }
    

    and you can start the interval timer for the GetClock function in the initMap function.

    proof of concept fiddle

    screenshot of clock output in InfoWindow

    code snippet:

    // This example displays a marker at the center of Australia.
    // When the user clicks the marker, an info window opens.
    
    function initMap() {
      var uluru = {
        lat: -25.363,
        lng: 131.044
      };
      var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 4,
        center: uluru
      });
      var MiamiContent =
        '<h3> Miami </h3><br ><h5><span id="clock"></span></h5>'
    
      var MiamiInfoCard = new google.maps.InfoWindow({
        content: MiamiContent
      });
    
    
      var marker = new google.maps.Marker({
        position: uluru,
        map: map,
        title: 'Uluru (Ayers Rock)'
      });
      marker.addListener('click', function() {
        MiamiInfoCard.open(map, marker);
      });
      setInterval(GetClock, 1000);
    }
    /* Always set the map height explicitly to define the size of the div
     * element that contains the map. */
    #map {
      height: 100%;
    }
    
    /* Optional: Makes the sample page fill the window. */
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <div id="map"></div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
    </script>
    <script>
      tday = new Array("Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat");
      tmonth = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sept", "Oct", "Nov", "Dec");
    
      function GetClock() {
        var d = new Date();
        var nday = d.getDay(),
          nmonth = d.getMonth(),
          ndate = d.getDate(),
          nyear = d.getYear(),
          nhour = d.getHours(),
          nmin = d.getMinutes(),
          nsec = d.getSeconds(),
          ap;
        if (nhour == 0) {
          ap = " AM";
          nhour = 12;
        } else if (nhour < 12) {
          ap = " AM";
        } else if (nhour == 12) {
          ap = " PM";
        } else if (nhour > 12) {
          ap = " PM";
          nhour -= 12;
        }
    
        if (nyear < 1000) nyear += 1900;
        if (nmin <= 9) nmin = "0" + nmin;
        if (nsec <= 9) nsec = "0" + nsec;
    
        console.log(tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear + " " + nhour + ":" + nmin + ":" + nsec + ap + "")
        var clockSpan = document.getElementById('clock');
        if (!!clockSpan) {
          clockSpan.textContent = tday[nday] + ", " + tmonth[nmonth] + " " + ndate + ", " + nyear + " " + nhour + ":" + nmin + ":" + nsec + ap + "";
        }
      }
    
    </script>