Saturday 26 March 2011

Loading Markers data from an XML file


XML
If you are going to use dozens of markers on your page, it will become difficult to manage them if you code them all in your Javascript as in the Described earlier. The preferred method for handling large numbers of markers is to store the data in an XML file. It's also possible to fetch data from database which returns XML containing a different selection from the data depending on query information derived from what your users input. I'm not going to cover this side of things in this tutorial.
The code used GDownloadUrl() to send a request to read the file.
Once the data has been read, the callback function can grab a collection of data from the XML tags
 var markers = xmlDoc.documentElement.getElementsByTagName("marker");
then for each of those tags extract the individual attribute fields
 var lat = parseFloat(markers[i].getAttribute("lat"));
Once all the data has been parsed, we can create the markers as in the previous examples.
XML attributes strings can't contain the characters < or >. You have to use &lt; and &gt;, which will get converted to < and > as the XML data is read.
Instead of using XML attributes, it's possible to lay out your XML data like this:
   <markers>
     <marker lat="67.65654" lng="24.90138" label="Marker One">
      <infowindow>Some stuff to display in the&lt;br&gt;First Info Window</infowindow>
     </marker>
   </markers>
Or even like this, using CDATA:
   <markers>
     <marker lat="67.65654" lng="24.90138" label="Marker One">
      <infowindow><![CDATA[
        Some stuff to display in the<br>First Info Window
      ]]></infowindow>
     </marker>
   </markers>
When using CDATA, is is not necessary to use &lt; and &gt; in the contained HTML.

XML formatted like this can be read with GXml.value, like this
   var html = GXml.value(markers[i].getElementsByTagName("infowindow")[0]);

No comments:

Post a Comment