我想在时钟到达晚上10点时更改标记图标,并在上午6点返回相同的图标..如何从某个地方选择正确的时钟?以及如何将图标改为这样的时间 谢谢!!
我的代码
function initialize() {
var mapDiv = document.getElementById('map-canvas');
var map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(-22.968015,-43.183161),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP });
var green = 'images/VERDE.png';
var yellow = 'images/AMARELO.png';
var red = 'images/VERMELHA.png';
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-22.967311,-43.186073),
icon: green});
答案 0 :(得分:1)
以下代码会根据时间更改标记。
var now=new Date().getUTCHours();
now -= 2;//Set for Rio Adjust to your time zone
var startTime = 22;//10 PM
var finishTime = 6;//6 AM
var icon;
function initialize() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: new google.maps.LatLng(-22.968015,-43.183161),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP });
var green = 'images/green.png';
var yellow = 'images/yellow.png';
var red = 'images/red.png';
//Choosing icon by time
if(now >= finishTime&&now < startTime){
icon = green;
}else {
icon = red;
}
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-22.967311,-43.186073),
icon: icon});
}
注意事项。
now -= 2
以适应当地时间并保存冬季。