【问题标题】:How to display a clock on a google maps infowindow如何在谷歌地图信息窗口上显示时钟
【发布时间】:2020-04-27 02:26:33
【问题描述】:

我正试图在我的信息窗口上显示一个时钟。当我登录网络控制台时,时钟运行良好,但每当我尝试在我的信息窗口上显示它时,它就无法工作,它会显示“未定义”。

我尝试添加 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 变量中调用函数的方式有问题,因为该函数确实在我的控制台中工作。或者是因为我将它记录在我的函数中,而信息窗口不知道如何“记录”事物。非常感谢您的帮助

【问题讨论】:

    标签: google-maps-api-3 infowindow


    【解决方案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 + "";
        }
      }
    

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

    proof of concept fiddle

    代码 sn-p:

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

    【讨论】:

    • 它有效,但需要一秒钟才能显示出来。是否可以在单击标记时立即显示时间?
    • 是的。一种选择是在 InfoWindow domready 事件上调用 GetClock 函数。
    猜你喜欢
    • 2020-04-26
    • 2016-07-03
    • 2016-12-21
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多