Gxml.parse谷歌地图从v2迁移到v3

时间:2014-03-18 19:19:58

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

我想在我正在开发的其中一个应用程序中将Google地图从v2移动到v3。我看到了这个链接Tips for Upgrading Gmaps v2 to v3 more quickly。但是,我无法在v2 API中找到GXml.parse方法的替代方法。

我正在使用此脚本http://gmaps-samples-v3.googlecode.com/svn/trunk/xmlparsing/util.js提供的downloadUrl函数。

    function renderMap(mapControlFile) {   
            if (!mapControlFile || !xmlPath) {
                downloadUrl(mapConfigFile, function(data,responseCode) {

                    try {errorTest = errorTest;}
                    catch(e) {errorTest = null;}
                    if (responseCode!=200 || errorTest=='yes') {
                        document.getElementById('mapCanvas').innerHTML = mapDataError; 
                        document.getElementById('mapCanvas').innerHTML += '<p>Response Code: '+responseCode+'</p>'; 
                        return;
                    }
// The below line uses GXml which is now deprecated in v3
                    xmlDocument = GXml.parse(data);
                    //process config element -- restricted to 1 config element in the xml file
                    configElement = xmlDocument.documentElement.getElementsByTagName("config");
                    //check for xml file parsing errors
                    if (configElement.length==0) {
                        document.getElementById('mapCanvas').innerHTML = mapDataError; //<div> tag in html document
                        return;
                    }
                    //try-catch handler for potential undeclared variable "auth" across all browsers (controls access during maintenance)
                    try {auth = auth;}
                    catch(e) {auth = null;}
                    //try-catch handler for potential undeclared variable "maintenanceTest" across all browsers (used for maintenance message testing)
                    try {maintenanceTest = maintenanceTest;}
                    catch(e) {maintenanceTest = null;}
                    //check if campus map system is offline for maintenance
                    if ((GXml.value(configElement[0].getElementsByTagName("offline")[0]) && auth==null) || maintenanceTest=='yes') {
                        document.getElementById('mapCanvas').innerHTML = GXml.value(configElement[0].getElementsByTagName("offlineMsg")[0]); //<div> tag in html document
                        return;
                    }
                    //initialize and load default map
                    xmlPath = GXml.value(configElement[0].getElementsByTagName("xmlPath")[0]);
                    overlayURL = GXml.value(configElement[0].getElementsByTagName("overlayURL")[0]); //set global variable
                    //create map and add controls (documentation at http://code.google.com/apis/maps/documentation/)
                    //map canvas is styled in ../css/campusmaps.css
                    //Google Maps API Version 2
                    map = new GMap2(document.getElementById("mapCanvas")); //<div> tag in html document
                    eval('map.setMapType('+GXml.value(configElement[0].getElementsByTagName("mapType")[0])+')');
                    map.addControl(new GSmallZoomControl3D());
                    zoomLevelMessage = GXml.value(configElement[0].getElementsByTagName("zoomLevelMessage")[0]); //set global variable
                    parseXml(xmlPath+GXml.value(configElement[0].getElementsByTagName("defaultMap")[0])); //function call to parse xml default map control file
                }); //end downloadUrl()
            } //end initialize and load default map
            else {
                parseXml(xmlPath+mapControlFile); //function call to parse xml map control files
            } //end load all non-default maps

    } //end renderMap()

1 个答案:

答案 0 :(得分:1)

你有一些选择。

  1. 使用像jquery这样的第三方库。
  2. 使用GXml.js
  3. 编写一个xml解析例程,就像我的downloadxml.js
  4. 版本一样

    选项2:

    function parse(textDoc){
       try{
          if(typeof ActiveXObject!="undefined"&&typeof GetObject!="undefined"){
             var b=new ActiveXObject("Microsoft.XMLDOM");
             b.loadXML(textDoc);
             return b;
          }else if(typeof DOMParser!="undefined"){
             return(new DOMParser()).parseFromString(textDoc,"text/xml");
          }else{
             return Wb(textDoc);
          }
       }
       catch(c){
          P.incompatible("xmlparse");
       }
       try{
          return Wb(textDoc);
       }
       catch(c){
          P.incompatible("xmlparse");
          return document.createElement("div");
       }
    }
    

    function parse(textDoc){ try{ if(typeof ActiveXObject!="undefined"&&typeof GetObject!="undefined"){ var b=new ActiveXObject("Microsoft.XMLDOM"); b.loadXML(textDoc); return b; }else if(typeof DOMParser!="undefined"){ return(new DOMParser()).parseFromString(textDoc,"text/xml"); }else{ return Wb(textDoc); } } catch(c){ P.incompatible("xmlparse"); } try{ return Wb(textDoc); } catch(c){ P.incompatible("xmlparse"); return document.createElement("div"); } }

    选项3:

    function xmlParse(str) {
      if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
        doc.loadXML(str);
        return doc;
      }
    
      if (typeof DOMParser != 'undefined') {
        return (new DOMParser()).parseFromString(str, 'text/xml');
      }
    
      return createElement('div', null);
    }