Creating your First Map | |||||||||||||||||||||
To get you started with FusionMaps XT, we will show you how to build a World Map to show the Population Distribution across continents. Once completed, the map will look as under (with animation and interactivity, which is not reflected by the image below):
If you're a non-technical user and want to generate maps without using any programs or scripts, you can switch to the Quick Map Configuration > Using Visual Map Builder page, where we explain how to visually configure and generate a data-driven map, without getting into any programming. Code examples discussed in this section are present in Download Package > Code > MyFirstMap folder. Follow the steps below to get started:
And that completes the recipe! Open the file FirstMap.html in a Web Browser » Internet Explorer, Firefox, Opera or Safari (Mac/iPhone/iPad/Windows) and you will see an animated World Map similar to the one below:
Rendering the map as JavaScript: The same code can render the map as JavaScript. You just have to add a line of code FusionCharts.setCurrentRenderer('javascript'). This code will ask FusionCharts renderer to skip the Flash rendering and create pure JavaScript maps. Click here to see the complete code and a live example. |
|||||||||||||||||||||
How it works? | |||||||||||||||||||||
Now that you've already created a map, let's get behind the scenes and understand how FusionMaps XT renders maps in a web-page. Essentially, to create a map, you need the following four items:
What happens if Flash player is not available? |
|||||||||||||||||||||
Explanation of map data | |||||||||||||||||||||
Let's take a closer look at the data and its XML form: |
|||||||||||||||||||||
|
|||||||||||||||||||||
Basically, what we have done above can be listed in the following points:
A Map Specification sheet provides a complete list of the Internal Id, Short Names and the Long Names associated with a particular map. Click here to view the map specification sheet of the world map. Just as a measure to check if the XML document is structurally valid, open the file in your browser. You should be able to see the XML data document in a formatted way, without any errors.Note that you can also provide map data in JSON format. You can view an example of JSON data here. Or, to learn more about FusionMaps XT JSON data format, please go through FusionMaps XT data formats > JSON section. |
|||||||||||||||||||||
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. To know more click here. |
|||||||||||||||||||||
Explanation of HTML & JavaScript code used to embed the map | |||||||||||||||||||||
In the HTML wrapper file (or your web page), the shots are called by the included JavaScript class file FusionCharts.js. Let's take a second look at the HTML code to gain an understanding of how it works. <html> <head> <title>My First map using FusionCharts JavaScript Class</title> <script type="text/javascript" src="Maps/FusionCharts.js"></script> </head> <body> <div id="mapContainer">FusionMaps XT will load here!</div> <script type="text/javascript"><!-- FusionCharts.setCurrentRenderer('javascript'); var myMap = new FusionCharts ("Maps/FCMap_World.swf", "MyMapId", "750", "400", "0"); myMap.setXMLUrl ("Data.xml"); myMap.render("mapContainer"); // --> </script> </body> </html> |
|||||||||||||||||||||
In the very beginning, the JavaScript class file FusionCharts.js is included into the HTML using the code shown below. The FusionCharts.js is smart enough to automatically load the other required JavaScript files - jquery.min.js, FusionCharts.HC.js and FusionCharts.HC.Maps.js on-demand. |
|||||||||||||||||||||
<script type="text/javascript" src="Maps/FusionCharts.js"></script> Existing users: You might be wondering what happened to FusionMaps.js which you have been using in your application. Starting FusionMaps XT, use of FusionMaps.js is deprecated. Use FusionCharts.js instead. But, although deprecated, to ease your upgrade process to FusionMaps XT, we have provided a new FusionMaps.js in FusionMaps XT pack's > Maps folder. This FusionMaps.js will help in loading FusionCharts.js (located in the same folder as FusionMaps.js) and all related JavaScript files in your application. However, we recommend that you replace all references to FusionMaps.js to FusionCharts.js in your code. |
|||||||||||||||||||||
Next, an HTML DIV with id mapContainer is created in the body of the page. | |||||||||||||||||||||
<div id="mapContainer">FusionMaps XT will load here!</div> |
|||||||||||||||||||||
Now comes the essential part, which instantiates the map. This is called the map constructor. |
|||||||||||||||||||||
var myMap = new FusionCharts( "Maps/FCMap_World.swf", "myMapId", "750", "400", "0" ); |
|||||||||||||||||||||
Here, myMap is the name of the JavaScript object (variable) that contains reference to the map. The following parameters are passed on to the myMap object:
Existing users: You might be wondering what happened to the sixth parameter - registerWithJS that comes after debugMode. Starting FusionMaps XT, use of registerWithJS is deprecated. It is always set to 1 internally. Although deprecated, you can continue to use this parameter in your existing code without any problem. The code below provides the reference (relative URL) of map data file (XML in this case). The path of the XML file is set using setXMLUrl() function as shown below: |
|||||||||||||||||||||
myMap.setXMLUrl( "Data.xml" ); |
|||||||||||||||||||||
The code sample above uses URL of static XML file. Ideally, you would not use a physical data file. Instead you will have your own server side scripts to use dynamic data and build XML. You can always provide the URL of the script to virtually relay the dynamically built data. Finally, the render() method is called and the ID of HTML DIV, where the map is meant to be rendered, is provided. This line of code renders the desired map inside the DIV. |
|||||||||||||||||||||
myMap.render( "mapContainer" ); |
|||||||||||||||||||||
Important Note: The map ID as well as the ID of the DIV element (or any HTML element where a map is meant to be rendered) should be unique. Also, if you are using global variables to store the JavaScript references of maps, make sure that the name of each such variable is unique. The map ID, the HTML element ID and the JavaScript variable name should not conflict with each other. Existing users: You might be wondering what happened to the functions like setDataURL()which you have already been using in your application. Yes - although deprecated, it will continue to work without any problem. |
|||||||||||||||||||||
Compact rendering method | |||||||||||||||||||||
Since FusionMaps XT, alternate methods of declaring and rendering a map using JavaScript are also available. You can use a compact single-line of JavaScript (instead of the three lines we saw in the above sample) to render a map as shown below: |
|||||||||||||||||||||
<html> <head> <title>My First map using FusionCharts JavaScript Class</title> <script type="text/javascript" src="Maps/FusionCharts.js"></script> </head> <body> <div id="mapContainer">FusionMaps XT will load here!</div> <script type="text/javascript"><!-- var myMap = FusionCharts.render( "Maps/FCMap_World.swf", "myMapId", "750", "400", "mapContainer", "Data.xml" ); // --> </script> </body> </html> See it live! |
|||||||||||||||||||||
The above code uses the static render function of the global FusionCharts object. This function takes all the required parameters to render a map like the map SWF , map ID, width & height of map, div ID where map will be rendered and reference to map data.
There are additional ways of rendering a map as well, which have been explained in Creating maps page in "FusionMaps XT and JavaScript" section. |
|||||||||||||||||||||
Troubleshooting | |||||||||||||||||||||
If for any reason, you do not see a map, run through the following checks: If you see an endless loading progress bar in your browser, or if the right click menu (right click at the place where the map is supposed to be) shows "Movie not loaded", check the following:
If you get an "Error in Loading Data." message, check the following:
If you get an "Invalid Data." message, it means that the XML/JSON data is malformed. Check it again for common errors like:
To check whether your final XML is ok, open it in your browser and you'll see the error. If only the text "FusionMaps XT will load here!" is displayed, check with the following:
|