douxie2007 2015-01-01 13:35
浏览 53
已采纳

谷歌地图TypeError:xml未定义

I'm currently developing a web application that uses google maps and ajax. I'm both new to the technologies so I'm not sure if I'm doing things right. I followed the instructions on how to create one at https://developers.google.com/maps/articles/phpsqlsearch_v3. here's the snippet of my code:

function searchNearLocations(radius){
        var searchUrl = './designIncludes/phpLogicIncludes/searchMarkers.php?lat=' + myLat +'&lng=' + myLng + '&radius=' + radius;
        downloadUrl(searchUrl, function(data) {
            var xml = parseXml(data);
            var markerNodes = xml.documentElement.getElementsByTagName("marker");
            var bounds = new googlemaps.LatLngBounds();
            for (var i = 0; markerNodes.Length; i ++){
                var name = markerNodes[i].getAttribute("name");
                var address = markerNodes[i].getAttribute("address");
                var info = markerNodes[i].getAttribute("info");
                var tts = markerNode[i].getAttribute("tts");
                var slatlng = new google.maps.LatLng(
                    parseFloat(markerNodes[i].getAttribute("lat")),
                    parseFloat(markerNodes[i].getAttribute("lang")));

                createOption(name,distance,i);
                createMarker(latlng,name,address);
                bounds.extend(latlng);
            }
        map.fitBounds(bounds);              
        });
    };

function createMarker(slatlng,name,address,info,tts){
    var html = "<b>" + name + "</b></br><u>" + address + "</u></br>" + info + "</br>" + "Time allowance to spend: " + tts;
    var marker = new google.maps.Marker({
        map:map,
        position:slatlng
    });
    google.maps.event.addListener(marker,'click',function(){
        infoWindow.setContent(html);
        infoWindow.open(map,marker);
    });
    markers.push(marker);
};

function downloadUrl(url,callback){
    var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') : 
        new XMLHttpRequest;

    request.onreadystatechange = function() {
        if(request.readyState == 4){
            request.onreadystatechange = doNothing;
            callback(request.responseText, request.status);
        }
    };
    request.open('GET',url,true);
    request.send(null);     
};

function parseXml(str){
    if (window.ActiveXObject){
        var doc = new ActiveXObject('MicrosoftXMLDOM');
        doc.loadXML(str);
        return doc;
    }else if (window.DOMParse){
        return (new DOMParser).parseFromString(str,'text/xml');
    }
};

function doNothing(){};    

I don't know why but when i use the console window of Mozilla I always get the error code xml is undefined.. i don't know what to do because I am really new to both technologies specially javascript. When i click the error i points me to

var markerNodes = xml.documentElement.getElementsByTagName("marker");  

I don't even know how to check if my php script is working right. I'm using mysqli instead of the mysql on the google map tut link above. I hope that you guys can help me. If this is a duplicate then please send me a link.

  • 写回答

1条回答 默认 最新

  • doupacan2098 2015-01-01 14:57
    关注

    Your parseXml function will return null if neither window.ActiveXObject nor window.DOMParse exists

    function parseXml(str){
        if (window.ActiveXObject){
            var doc = new ActiveXObject('MicrosoftXMLDOM');
            doc.loadXML(str);
            return doc;
        }else if (window.DOMParse){
            return (new DOMParser).parseFromString(str,'text/xml');
        }
    };
    

    You have a typo DOMParse should be DOMParser (like it is in the tutorial you reference)

    function parseXml(str){
        if (window.ActiveXObject){
            var doc = new ActiveXObject('MicrosoftXMLDOM');
            doc.loadXML(str);
            return doc;
        }else if (window.DOMParser){
            return (new DOMParser).parseFromString(str,'text/xml');
        }
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行