How to use FusionMaps? | |||||||||||||||||||||
In this section, we'll show how to use FusionMaps in your web applications. As you already know, FusionMaps uses XML data files to render the maps. As such, you can use FusionMaps with any of your dynamic web scripting languages like ASP, .NET, PHP, JSP, ColdFusion, Ruby on Rails etc. From your scripting language, all you would need to do is dynamically generate the XML data document (containing data from your data sources), and then convey it to FusionMaps. Here, for the purpose of learning, we'll hand code the the XML data files. When working with scripting languages, you can replace this process with scripts that dynamically build XML data document in variables (using either String concatenation or native XML Object methods). If you're a non-technical user and want to generate maps without using any programs or scripts, you can switch to the next section "Using the GUI", where we explain how to visually configure and generate a data-driven map, without getting into any programming. Let's now jump to our example. For our first example, we'll create a World Map to show the Population Distribution across continents. Our motive is to display the following data visually on a map. |
|||||||||||||||||||||
Population in Millions |
|||||||||||||||||||||
|
|||||||||||||||||||||
Though it wouldn't serve a great scientific purpose to show this data on a map (instead of a table), it would serve our purpose of a demo. As we proceed, you'll later see how maps can make data interpretation way easier than simple data tables. | |||||||||||||||||||||
Assembling the required elements |
|||||||||||||||||||||
To create any map, you need to make sure that you've the following things ready with you:
Let's create a new folder for this example. We name the folder as HowToUse. In the download package, it's present under Code folder. Copy FCMap_World.swf file in this folder along with FusionMaps.js. That completes our initial setup for the map. We'll next see how to create the XML data for this map. But, before that, let's just glance through the Map Specification Sheet of the world map, as this will be required to create the XML data. |
|||||||||||||||||||||
Map Specification Sheet for our World Map | |||||||||||||||||||||
The Entities table for the world Map is reproduced below (from the World Map Specification Sheet). |
|||||||||||||||||||||
An entity for a map is the smallest item that we're dealing with on the map. For example, in a World Map showing continents, each continent is an entity. In a continent map showing countries, each country is referred to as an entity. Similarly, if a country map is showing states, each state is referred to as an entity. |
|||||||||||||||||||||
|
|||||||||||||||||||||
As you can see above, each entity on the map has three properties:
|
|||||||||||||||||||||
You can define your own IDs, short names and long names for each entity – we'll see that later. |
|||||||||||||||||||||
Creating the XML File for our example | |||||||||||||||||||||
Since this is our first example, we'll try and put up a basic map to show the population data. Create a file FirstData.xml with the following XML: |
|||||||||||||||||||||
<map borderColor='005879' fillColor='D7F4FF' numberSuffix='
Mill.' includeValueInLabels='1' labelSepChar=': ' baseFontSize='9'> |
|||||||||||||||||||||
In the above code, we're basically providing the tabular data to map in XML format, as FusionCharts FusionMaps accepts only XML. Each XML data document for FusionMaps starts with <map> element. There are a number of cosmetic and functional attributes which you can specify for <map> element. You can see them in "FusionMaps and XML > XML Attributes" section. For this example, we've defined the border color of the map, fill color, number suffix (to be added at the end of every number), a flag to include data values in map labels, the label separator character and font size attributes. Immediately inside the <map> element, we've defined <data> element, whose child nodes contain the actual data. To define data for each entity on the map, an <entity> element is added as under: <entity id='NA' value='515' /> The id here corresponds to the internal Id of the entity (picked from Map Specification Sheet). value denotes the actual value which we want to show on the map. That completes the XML part of the application. Now, for the map to use this XML, we first need to put the map in an HTML page. We'll see that next. Using the Visual GUI provided in Download Package > Tools > GUI, you can automatically generate the Entity XML template for any map. That is, you would not have to manually write the <entity> element for each entity of the map. Instead our GUI will build this for you, and you can plug it in your code and then replace the values with values from your data store. |
|||||||||||||||||||||
Creating the HTML Container | |||||||||||||||||||||
Create a file FirstMap.html in the same folder with the following code: |
|||||||||||||||||||||
<html> <body>
|
|||||||||||||||||||||
Here, we're first including FusionMaps JavaScript Class file (FusionMaps.js). This file is required to include the map in an HTML page. You can also directly provide the HTML <OBJECT> code to embed the Flash movie for map, but then it would show the "Click to activate..." message in Internet Explorer. Also, using this class makes a lot of map actions very easy to invoke. <script language="JavaScript" src="FusionMaps.js"></script> Each map in the page needs to reside in a DIV. So, we create a DIV in the page body with a unique id (mapdiv in example above). <div id="mapdiv" align="center"> After that, we instantiate the map by creating an instance of FusionMaps class. var map = new FusionMaps("FCMap_World.swf", "Map1Id", "750", "400", "0", "0"); Here, map is the name of the JavaScript object that contains reference to our map. As parameters, we pass:
After this, we convey the path of XML data document to the map. To do this, we call the setDataURL() method of our map object. map.setDataURL("FirstData.xml"); Finally, we ask the map to render by calling render() method of the map object and specifying the Id of the DIV which we want the map to occupy. Using the FusionMaps Visual GUI provided in Download Package > Tools > GUI, you can automatically generate the HTML code for embedding the map. Time to see the efforts fructify - when you now run the HTML file, you should see a map as under. |
|||||||||||||||||||||
Very basic – but exactly what we had intended to. When you roll your mouse over any continent, you'll see the hover color and tool tip information as shown under: |
|||||||||||||||||||||
If you need to add any captions or additional information for the map, you can do so in HTML file. | |||||||||||||||||||||
Next, we'll see how to use the visual GUI for map generation. |