brio
brio

Reputation: 175

XML to JavaScript Array [Google Maps]

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 = [
                 new google.maps.LatLng(40.9921196514,47.8604733650 ),
                 new google.maps.LatLng(40.9922511293,47.8606186245 ),

        ];

Code

 downloadUrl("poly.php", function(data) {
        var xml = data.responseXML;
        var polys = xml.documentElement.getElementsByTagName("poly");
        for (var i = 0; i < polys.length; i++) {

             var pid  = polys[i].getAttribute("pid");


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

i`ve do that but it does not work.. ((

P.S. I get data from MySQL.

...

Xml:

    <polys>
<poly pid="1" pstatus="status1" plat="40.992638" plng="47.860474"/>
<poly pid="2" pstatus="status2" plat="40.992252" plng="47.860619"/>
</polys>

Upvotes: 0

Views: 419

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117314

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"))));

Upvotes: 1

Related Questions