刷新Google Maps V3中的KML数据

时间:2013-07-08 17:13:59

标签: javascript google-maps google-maps-api-3

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

这是一个可以在V2中工作的工作版本。

V2 EXAMPLE

以下是我更新的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 CODE

V3 FULL PAGE

编辑:我认为这与我想要移除的代码片段有关,然后将更新后的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
                  });

               }

2 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分:1)

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

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