如何在 google 地图信息窗口中显示时钟
How to display a clock on a google maps infowindow
我正在尝试在我的信息窗口中显示一个时钟。当我登录网络控制台时,时钟运行良好,但每当我尝试在我的信息窗口上显示它时,它都不起作用,它会显示 "undefined".
我尝试添加 GetClock()
函数,returns 时间是这样的:
var MiamiContent =
'<h3> Miami </h3><br ><h5>'+ setInterval(GetClock, 1000) +' </h5>'
var MiamiInfoCard = new google.maps.InfoWindow
({
content: MiamiContent
});
这是returns时间的函数。它工作正常。
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 变量中调用该函数的方式有问题,因为该函数在我的控制台中确实有效。或者是因为我将它记录在我的函数中,而信息窗口不知道如何 "log" 事情。非常感谢帮助
如果你想让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
函数启动间隔定时器
代码片段:
// 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>
我正在尝试在我的信息窗口中显示一个时钟。当我登录网络控制台时,时钟运行良好,但每当我尝试在我的信息窗口上显示它时,它都不起作用,它会显示 "undefined".
我尝试添加 GetClock()
函数,returns 时间是这样的:
var MiamiContent =
'<h3> Miami </h3><br ><h5>'+ setInterval(GetClock, 1000) +' </h5>'
var MiamiInfoCard = new google.maps.InfoWindow
({
content: MiamiContent
});
这是returns时间的函数。它工作正常。
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 变量中调用该函数的方式有问题,因为该函数在我的控制台中确实有效。或者是因为我将它记录在我的函数中,而信息窗口不知道如何 "log" 事情。非常感谢帮助
如果你想让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
函数启动间隔定时器
代码片段:
// 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>