Question

I'm loading a map with geoxml3. if I use a kml file or a xml string, It works fine

KML Version (OK)

geoXml_1.parse('kmlFile.kml');
if (geoXml_1.docs[0].gpolygons.length>0){ //NO ERROR IN geoXml_1.docs[0]
    [.....]
}else{
   [.....]
}

XML String

geoXml_1.parseKmlString("<?xml version='1.0' encoding='UTF-8'?><kml xmlns='http://earth.google.com/kml/2.0'><Placemark><name>Manfred Mustermann</name><description>Manfred Mustermann</description><Point><coordinates>7.0964850607874,51.781641735074,0</coordinates></Point><address>Musterstr 29 Aachen, 52070 Nordrhein-Westfalen</address><styleUrl>#0</styleUrl></Placemark></kml>")
if (geoXml_1.docs[0].gpolygons.length>0){ //NO ERROR IN geoXml_1.docs[0]
    [.....]
}else{
   [.....]
}

but if I use a asp file to write a xml, it doesn.t works fine. I get a javascript error:

geoXml_1.parse('/service/map.asp');

Where map.asp return a XML:

/service/map.asp

<%
Response.ContentType = "text/xml"
response.write "<?xml version='1.0' encoding='UTF-8'?><kml xmlns='http://earth.google.com/kml/2.0'><Placemark><name>Manfred Mustermann</name><description>Manfred Mustermann</description><Point><coordinates>7.0964850607874,51.781641735074,0</coordinates></Point><address>Musterstr 29 Aachen, 52070 Nordrhein-Westfalen</address><styleUrl>#0</styleUrl></Placemark></kml>"
%>

With firebug the output is OK. I can see the xml, but I get error here:

geoXml_1.parse('/service/map.asp');
if (geoXml_1.docs[0].gpolygons.length>0){ //ERROR ON FIREBUG: geoXml_1.docs[0] is undefined 
    [.....]
}else{
   [.....]
}

Does geoxml accept I want to do really? Is it possible?? Why not?

Thanks!!

Was it helpful?

Solution

parse uses AJAX to request the KML-document, you can't access the docs immediately after the call of parse, because AJAX runs asynchronously.

Use the afterParse-callback:

geoXml_1 = new geoXML3.parser(
  {/* options ,*/
   afterParse:function(docs){
     if (docs[0].gpolygons.length>0){ 
        //[.....]
    }else{
        //[.....]
    }
   }
  });

geoXml_1.parse('/service/map.asp');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top