javascript - 刷新 Google map V3 中的 KML 数据

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

我有一张 map ,已从 V2 移植到 V3,并且我正在尝试更新代码以在设定的时间刷新 KML 数据。在本例中每 30 秒一次。它只是假设更新 map 上的数据并显示下次更新发生的倒计时。

这里是它在 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 map 代码的链接。

V3 NSGAMP CODE

V3 FULL PAGE

编辑:我认为这与这段代码有关,该代码段应该删除然后将更新的 KML 数据添加到 map 中。

这是 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
                  });

               }

最佳答案

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

在 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 中已被贬值,所以我花了一些时间才找到替代品。

关于javascript - 刷新 Google map V3 中的 KML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17532345/

相关文章:

javascript - Openlayers 设置图层索引

javascript - 如何获取对表格内文本区域的引用?

javascript - 带有谷歌地图事件监听器的类方法运行外部函数

android - 如何从Android中的经纬度获取地址?

php - 如何在jquery中获取PHP $_SESSION数组的值?

javascript - 当变量改变时改变功能上的声音效果

google-maps - MonoTouch 获取位置并捕获 map 图像(例如联系人)

php - 使用 PHP 解析 Google 海拔高度的结果

android - 我想过滤位置 (LatLng)

javascript - 每个标记的 InfoBox (getElementById) – Google Maps API 3