FusionMaps, JavaScript and getXML() Method |
You can get the XML data of the maps generated by FusionMaps at client side using the getXML() method of each map. In this section, we'll show you how to attain this. The first step is to set registerWithJS flag of map as 1, as shown under: |
<div id="map1div"> |
Thereafter, you can invoke the getXML() method of the map object to print it. |
<HTML> <HEAD> <TITLE>FusionMaps & JavaScript - Getting Map XML Example</TITLE> <SCRIPT LANGUAGE="Javascript" SRC="../../FusionMaps/FusionMaps.js"></SCRIPT> <SCRIPT LANGUAGE="JavaScript"> function getMapXML(){ //Get map from its ID var mapToPrint = getMapFromId("map1Id"); alert(mapToPrint.getXML()); } </SCRIPT> </HEAD> <BODY> <CENTER> <h2>FusionMaps and JavaScript - getXML() Example</h2> <div id="map1div"> FusionMaps </div> <script language="JavaScript"> var map1 = new FusionMaps("../../FusionMaps/FCMap_World.swf", "map1Id", "400", "300", "0", "1"); map1.setDataXML("<map>...data here... </map>"); map1.render("map1div"); </script> <center><input type='button' value='Show Map XML' onClick='javascript:getMapXML();'></center> </CENTER> </BODY> </HTML> |
In the above code, we're first creating a map with DOM Id as map1Id. We also register it with JavaScript. Thereafter, we've created a HTML button, which when clicked invokes the local getMapXML() function. This function just gets the reference to the map using getMapFromId() function and finally invokes the getXML() method on the map. If you're specifying the data of the map using dataURL, make sure to call getXML() only in FC_Rendered event of the map, as before that the map will not return the correct XML.
|
Getting <map> attribute value |
If you wish to get the attribute value specified for <map> element in the XML, you can use getMapAttribute(attributeName) API as under: |
<HTML> <HEAD> <TITLE>FusionMaps & JavaScript - Getting Map Attribute Example</TITLE> <SCRIPT LANGUAGE="Javascript" SRC="../../FusionMaps/FusionMaps.js"></SCRIPT> <SCRIPT LANGUAGE="JavaScript"> function getMapAttrib(){ //Get map from its ID var mapToPrint = getMapFromId("map1Id"); alert(mapToPrint.getMapAttribute('bgColor')); } </SCRIPT> </HEAD> <BODY> <CENTER> <h2>FusionMaps and JavaScript - getMapAttribute() Example</h2> <div id="map1div"> FusionMaps </div> <script language="JavaScript"> var map1 = new FusionMaps("../../FusionMaps/FCMap_World.swf", "map1Id", "400", "300", "0", "1"); map1.setDataXML("<map bgColor='FF5904' >...data here... </map>"); map1.render("map1div"); </script> <center><input type='button' value='Get Map BgColor onClick='javascript:getMapAttrib();'></center> </CENTER> </BODY> </HTML> |
Here, we've first defined the bgColor attribute of map in XML as 'FF5904'. Thereafter, we've defined a button which when clicked calls the getMapAttribute('bgColor') method to retrieve the caption. If the attribute was not initially specified in XML or an empty value was specified, the map returns "null" value. |