Google Maps API Tutorial© 2006, 2007, 2008, 2009 Mike Williams |
Translate this page: |
XMLIf you want to have dozens of markers on your page, it gets a bit cumbersome to manage them if you code them all in your Javascript as in the previous examples.The preferred method for handling large numbers of markers is to store the data in an XML file. It's also possible to have an application (e.g. mySQL) running on your server 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 the mySQL side of things in this tutorial. Here's a simple example You can see the XML data file that I used here The code used GDownloadUrl() to send a request to read the file. At some later time, the reply comes back and the callback funtion gets invoked. 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 and the sidebar as in the previous examples. Note that the code to insert the assembled sidebar information into its <div> must be inside the callback function. XML attributes strings can't contain the characters < or >. You have to use < and >, 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="43.65654" lng="-79.90138" label="Marker One"> <infowindow>Some stuff to display in the<br>First Info Window</infowindow> </marker> </markers>Or even like this, using CDATA: <markers> <marker lat="43.65654" lng="-79.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 < and > 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]); Potential Pitfalls
|