谷歌地图Ajax调用:v2和v3之间的差异


Google maps Ajax call: differences between v2 and v3

我正在将谷歌地图v2项目升级到v3。到目前为止进展顺利,但我遇到了一个我还没有找到解决方案的问题(尽管我确实有一个解决方法)。

地图的访问者可以单击一个位置,让Lat&Long出现在信息窗口中,并能够将该数据保存到XML文件中。我在 v2 中工作正常。

它在 v3 中也可以工作,但前提是我删除所有 Ajax 检查,这似乎很危险。在 v2 中,我有:

        var request = GXmlHttp.create();
// open the request to storeMarker.php on server
request.open('GET', 'storeMarker.php' + getVars, true);
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        // the request is complete
        var xmlDoc = request.responseXML;
        // retrieve the root document element (response)
        var responseNode = xmlDoc.documentElement;
        // retrieve the type attribute of the node
        var type = responseNode.getAttribute("type");
        // retrieve the content ofthe responseNode
        var content = responseNode.firstChild.nodeValue;
        //check to see if it was an error, or success
        if (type != 'success') {
            alert(content);
        } else {
            // create a new marker and add its info window
            var latlng = new GLatLng(parseFloat(lat), parseFloat(lng));
            var marker = makeTempMarker(latlng, content, bus);
            map.addOverlay(marker);
            map.closeInfoWindow();
        }
    }

不知何故,GXmlHttp.create()似乎已经处理了我称之为"storeMarker.php"的文件,而不是XML。storeMarker.php 文件确实会打开一个 XML 文件(并成功保存新标记)。这段代码来自"Beginning Google Maps Applications with PHP and Ajax"(Apress: Purvis, Sambells & Turner, 2006),它仅适用于 v2。

GXmlHttp.create() 不再可用于 v3,我改用了一个简短的跨浏览器函数来打开 Ajax 请求(此处未复制函数):

    var request = getAjaxObject();

现在JS在"request.responseXML"上窒息了。我已经尝试过"request.responseText",但(不出所料)这也不起作用。

我的问题是,我该怎么办?

正如我上面所说,如果我删除"status == 200"检查之外的响应检查,并使用适当的 v3 语法,标记数据将被保存并适当显示临时标记。但它感觉并不"安全"。

我现在发现'storeMarker.php'文件应该有一个标题'header('Content-Type: text/xml');'。(它在翻译中丢失了)。把它放回去让我进入脚本的下一行(!),但我希望我能从这里开始自己的方法解决问题。