XML to JavaScript Array [Google Maps]

后端 未结 1 636
天命终不由人
天命终不由人 2021-01-26 04:35

I need create Javascript array from xml ..

I`v get xml data from poly php with ajax. Everything is ok.

I must create array like that:

  point =          


        
相关标签:
1条回答
  • 2021-01-26 05:03

    May I assume you use the function downloadUrl from googles util.js ?

    When yes: data is already a document, there is no need to access data.responseXML

    Each attempt to access a property of xml will result in an error, because xml is undefined

    Replace this:

        var xml = data.responseXML;
        var polys = xml.documentElement.getElementsByTagName("poly");
    

    with:

        var polys = data.documentElement.getElementsByTagName("poly");
    

    There is an syntax-error:

    point = [
              new google.maps.LatLng(parseFloat(polys[i].getAttribute("plat")), parseFloat(polys[i].getAttribute("plng")) );
            ];
    

    remove the semicolon:

    ("plng")) );
    //---------^
    

    But to get the desired result you must create the point-array outside the loop:

    var point=[];
    

    and add the LatLng's to point inside the loop:

    point.push(new google.maps.LatLng(parseFloat(polys[i].getAttribute("plat")),  
                                      parseFloat(polys[i].getAttribute("plng"))));
    
    0 讨论(0)
提交回复
热议问题