2

我有一张从 V2 移植到 V3 的地图,我正在尝试更新代码以在设定的时间刷新 KML 数据。在这种情况下,每 30 秒一次。只是假设更新地图上的数据并显示下一次更新发生的倒计时。

这是它在 V2 中如何工作的工作版本。

V2 示例

这是我更新的 V3 脚本中的相关代码,但它不起作用。我没有收到任何错误,所以我不确定我做错了什么。这适用于 V2,但我无法让它与 V3 一起使用。我错过了什么,忽略了什么?

//This calls genkml.php on every refresh cycle to generate a new kml file
function UpdateKML() {
    //document.getElementById('TheDiv').innerHTML = '0';
    var xmlhttp=false;
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }
    xmlhttp.open("GET", "genkml.php?force=" + force + "&ofd=" + KMLdate + "&nsd=" + NSdate + "&dbg=" + dbg + "&rand="+(new Date()).valueOf(),true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
            var resp = xmlhttp.responseText;
            //if (resp === undefined) resp = '';        // If we get a bad response, just set resp to nothing
            if (dbg == 'y') {                           // Check if we want debug info
                var tmpresp = resp;
                if (tmpresp === undefined) tmpresp  = ' ';
                if (document.getElementById('div1') == null) {  // Check if debug div exists, if not add it to end of body
                    var divTag = document.createElement("div");
                    divTag.id = "div1";
                    divTag.innerHTML = 'Response Status: ' + xmlhttp.status + '<br />' + tmpresp;
                    document.body.appendChild(divTag);
                } else {                                        // Otherwise just update the info
                    document.getElementById('div1').innerHTML = 'Response Status: ' + xmlhttp.status + '<br />' + tmpresp;
                }
            } else {                                // Else check if debug div exists and remove it (will take an update to remove
                if (document.getElementById('div1') != null) document.body.removeChild(document.getElementById("div1"));
            }
            if (resp !== undefined) {   // Make sure we got data
                KMLdate = resp.split("|")[0].split("~")[0];
                NSdate = resp.split("|")[0].split("~")[1];
                updateHTML(resp);       // This calls the updateHTML function if there is info returned
            }
        }
    }
    xmlhttp.send(null);
    // add back overlays
    nyLayer = new google.maps.KmlLayer(null);
    nyLayer.setMap(null);               // Remove overlays
    var nyLayer = new google.maps.KmlLayer(URLToKML + "?rand="+(new Date()).valueOf(),
                  {
                      suppressInfoWindows: false,
                      map: map,
                      preserveViewport: true,
                      zIndex: 999
                  });

// Time overlayed on map - could be in updateHTML() to just show when .kml read last
    var time = CurrentTime ("B", "12a", true, TZOffset)
    document.getElementById('currenttime').innerHTML = time;

}

function CurrentTime (type, hours, secs, ofs) {
/*
type (char)           hours (char)      secs (bool)     ofs (num)
"U"-UTC               "24"=24 hr time   true=hh:mm:ss   0=hours from UTC
"B"-User's Browser    "12"=12 hr time   false=hh:mm
"S"-Web Site          "12a"=am/pm
*/
    if (type  == null){ type  = "B"; }     // These are the defaults
    if (hours == null){ hours = "12a"; }
    if (secs  == null){ secs  = true; }
    if (ofs   == null){ ofs   = 0; }
    var currentHour = 0;
    var currentMinute = 0;
    var currentSecond = 0;
    var time = 0;
    var currentDate = new Date();

    if (type == "U") {
        currentHour = currentDate.getUTCHours();                      // UTC
    } else if (type == "B") {
        currentHour = currentDate.getHours();                         // Viewer's time
    } else {
        currentHour = currentDate.getUTCHours() + ofs;                // your local time
        if(currentHour < 0) { currentHour = currentHour + 24;}
    }

    currentMinute = currentDate.getMinutes();
    currentMinute = (currentMinute < 10 ? "0" : "") + currentMinute;

    if (hours == "24") {
        if(currentHour == 24) { currentHour = 0 };                                 // use if wanting 24 hour time
        currentHour = (currentHour < 10 ? "0" : "") + currentHour;
    } else if (hours == "12") {
        if(currentHour == 0) currentHour = 12;
        currentHour = (currentHour < 10 ? "0" : "") + currentHour;
    } else {
        if(currentHour == 0) currentHour = 12;                                    // else no leading zero for am/pm
    }

    time = currentHour + ":" + currentMinute;

    if (secs) {
        currentSecond = currentDate.getSeconds();
        currentSecond = (currentSecond < 10 ? "0" : "") + currentSecond;
        time = time + ":" + currentSecond;
    }

    if (hours == "12a") {
        time = time + " " + (currentHour > 12 ? "PM" : "AM");
    }

    return time;
}

//This function is only used if you leave the debug checkbox below
//  You can remove this function and the checkbox and set the debug
//  mode using the dbg=y query parameter
function debug(obj){
    if (obj.checked) {
        dbg='y';
    } else {
        dbg='n';
        if (document.getElementById('div1') != null) document.body.removeChild(document.getElementById("div1"));
        //document.getElementById('TheDiv').innerHTML = '';
    }
}
//This function is only used if you leave the Force Update checkbox below
//  You can remove this function and the checkbox and set the force
//  mode using the force=y query parameter
function forceupdate(obj){
    if (obj.checked) {
        force='y';
    } else {
        force='n';
    }
}
//This function parses out the query parameter value
function gup( name ){
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

如果有人需要,这里是 V3 的完整 .js 地图代码的链接。

V3 NSGAMP 代码

V3 整页

编辑:我认为这与假设删除然后将更新的 KML 数据添加到地图的这段代码有关。

这是在 V2 中,现在已贬值。

// add back overlays
    map.removeOverlay(geoXml);              //Remove overlays
    geoXml = new GGeoXml(URLToKML + "?rand="+(new Date()).valueOf() );      //rand is used to trick google maps into thinking this is a new KML (don't use cache version)
    map.addOverlay(geoXml);                 //Add the new data from the newly generated KML

我为 V3 更新的代码替换了通过搜索找到的上述已折旧的 V2 代码段。不确定这是否正确,但这是我能找到的。

// add back overlays
    nyLayer = new google.maps.KmlLayer(null);
    nyLayer.setMap(null);               // Remove overlays

    function refresh(layer) {

    var nyLayer = new google.maps.KmlLayer(URLToKML + "?rand="+(new Date()).valueOf(),
                  {
                      suppressInfoWindows: false,
                      map: map,
                      preserveViewport: true,
                      zIndex: 999
                  });

               }
4

2 回答 2

1

我把它整理好并开始工作,我知道这是一个简单的编辑,但我很难找到解决方案。如果其他人可以帮助这就是我所做的。

在 V2 版本中对此进行了更改。

updateHTML(resp);       //This calls the updateHTML function if there is info returned
        }
    }
}
xmlhttp.send(null);
// add back overlays
map.removeOverlay(geoXml);              //Remove overlays
geoXml = new GGeoXml(URLToKML + "?rand="+(new Date()).valueOf() );      //rand is used to trick google maps into thinking this is a new KML (don't use cache version)
map.addOverlay(geoXml);                 //Add the new data from the newly generated KML

在 V3 版本中对此进行了说明。

updateHTML(resp);       // This calls the updateHTML function if there is info returned
            }
            //remove layer
            window.nyLayer.setMap(null);
            //change its url so that we would force the google to refetch data
            window.nyLayer.url = URLToKML + "?rand="+(new Date()).valueOf();
            //and re-add layer
            window.nyLayer.setMap(window.map);
        }
    }
    xmlhttp.send(null);

并且在 V3map.removeOverlaymap.addOverlay已贬值,所以我花了一段时间才找到替代品。

于 2013-07-11T01:42:19.240 回答
1

或者您可以进一步简化它并使用:

window.nyLayer.url = URLToKML + '&ver=' + Date.now();
于 2016-11-10T10:32:25.277 回答