Javascript 在Google Maps V3中刷新KML数据

Javascript 在Google Maps V3中刷新KML数据,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一个从V2移植到V3的映射,我正在尝试更新代码,以便在设定的时间刷新KML数据。在这种情况下,每30秒。只需更新地图上的数据,并显示下一次更新发生的倒计时 下面是它在V2中的工作方式 下面是我更新过的V3脚本中的相关代码,但它不起作用。我没有得到任何错误,所以我不知道我做错了什么。这在V2上运行,但我无法让它在V3上运行。我错过了什么,忽略了什么 //This calls genkml.php on every refresh cycle to generate a new kml fil

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

下面是它在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更新的代码I替换了通过搜索找到的上述折旧的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
                  });

               }

我对它进行了分类和工作,我知道这是一个简单的编辑,但我真的很难找到解决方案。如果有人能帮上忙,这就是我所做的

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

map.removeOverlay
map.addOverlay
在V3中折旧,因此我花了很长时间才找到替代品。

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

window.nyLayer.url = URLToKML + '&ver=' + Date.now();

你能提供一个到该页面的v3版本的链接吗?@geocodezip链接到完整的v3代码,包含代码的页面位于原始帖子的底部。NSGMAP.js代码通过
包含在页面标题中。我看到了完整的代码,如果没有指向完整页面的链接(您添加了该链接,谢谢),我无法运行该代码来查看问题。v3页面中的“cycle”函数的等价物在哪里?@geocodezip它位于我在原始帖子中发布的代码中。我更新了折旧后的V2代码,但显然这对于V3来说还不够。我已经做了搜索,但没有太多提到这一点。我已经将更新后的KML图层代码应用到我发现的应该更新的地方,但它不起作用。在哪里调用它?v2页面调用了“addLoadEvent(cycle)”,我在v3版本中没有看到类似的调用。
window.nyLayer.url = URLToKML + '&ver=' + Date.now();