如何在谷歌地图信息窗口上显示时钟

时间:2020-01-09 13:08:23

标签: google-maps-api-3 infowindow

我正在尝试在信息窗口中显示时钟。当我登录网络控制台时,时钟运行良好,但是每当我尝试在信息窗口中显示它时,它将无法工作,并且将显示“ undefined”。

我尝试添加GetClock()函数,该函数返回如下时间:

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

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

这是返回时间的函数。很好。

    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);
    }

我假设在MiamiContent变量中调用函数的方式有问题,因为该函数确实可以在我的控制台中工作。否则是因为我将其记录在函数中,而信息窗口却不知道如何“记录”事物。非常感谢帮助

1 个答案:

答案 0 :(得分:1)

如果您希望GetClock函数显示在InfoWindow的DOM中,则需要编写代码来做到这一点。例如:

var MiamiContent =
  '<h3> Miami </h3><br ><h5><span id="clock"></span></h5>'

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

然后在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 + "";
    }
  }

,您可以在GetClock函数中启动initMap函数的间隔计时器。

proof of concept fiddle

screenshot of clock output in InfoWindow

代码段:

// 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>