0

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>
6
  • please explain "does not work." Are you seeing errors? Strange behavior? What exactly is it doing which is different from what you expect? Commented Jan 5, 2013 at 19:54
  • I use Alert(point); dont show me anything. But when use point = [ new google.maps.LatLng(40.9921196514,47.8604733650 ), new google.maps.LatLng(40.9922511293,47.8606186245 ), ]; like that it work Commented Jan 5, 2013 at 20:10
  • please show us an example- <poly/>-element Commented Jan 5, 2013 at 20:33
  • ok. I`v edit question. At end added XML poly element Commented Jan 5, 2013 at 20:42
  • Example that parses XML to polylines (the XML is slighly different format from yours). Commented Jan 5, 2013 at 21:08

1 Answer 1

1

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"))));
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for answer. Bu didnt work :(( .. Myjs page like that jsfiddle.net/JFR3n ... [i`d include util.js in main page]
So thanks. I`l do it with xml point.push ..it work)) but interesting problem: (40.992638, 47.86047400000007),(40.992252, 47.86061900000004) it show me this lat long.. but my lat longs like that: (40.992638 47.860474 ) (40.992252 47.860619)..... what is: 00000007 & 00000004 ? where it take this numbers?:((
not an solution, but maybe interesting: stackoverflow.com/questions/13307066/…
no its solution.. i dont understand ., what is this numbers?:))
it must be the result of an internal calculation by the maps-API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.