FusionMaps and Flash > Advanced Examples |
In our previous examples, we had seen how to load FusionMaps in your Flash movies in a very basic way. In this advanced example, we'll assimilate all the information that you've gained from previous examples and use that with new concepts to create an interactive form based mapping application. We'll build a form that takes in continent-wise sales information and then plot it on a World Map. This application explains the following new concepts pertinent to FusionMaps:
If you're excited after just reading the possibilities above, let's inspire you more by showing how the application would look when it's finished: |
Initial state of the application, where the user enters sales data. |
Final map generated when the user enters requisite data and then clicks on "Plot It" button. Now that you're already jumping, let's straight jump into action. |
Setting the movie |
For this example, we'll create a new .fla and name it as FormExample.fla. We keep it under the same FlashExamples folder so that we do not have to make additional copies of com folder. In this application, we've three states.
Each state is represented by a key-frame as shown below. The name of state keyframes are FInit, FForm and FMap respectively. Shown below is a screenshot: |
As you can see above we've following additional layers in this movie:
|
Setting the form |
The form for this application looks as under. We've 6 text fields on this form to accept data for 6 continents. Apart from that, it also contains a few descriptive labels. |
Binding Actions |
Let's now switch our focus to the core of this application - ActionScripts which bind the entire application. We'll study the ActionScripts frame by frame. Let's first see the ActionScript contained in frame 1: |
/** #include "com/fusionmaps/includes/LoadingFunctions.as" //Import the Class of the map which you want to create. //Create container movie clip for map |
In this frame, we're basically including and importing the required files for map. We're also creating the empty movie clip for the map and then instantiating the WorldMap. The actions in this frame run only once during the entire application. So, we're effectively creating only one instance of WorldMap. We'll be updating the same instance with new data as and when the user changes the data in form. Let's see the code that runs the form in frame 2: |
/** //Set default values for text fields - just for demo. //Define the listener for "Plot" button's click event. //Add data to it //Stop till an action is invoked. |
Here, we're:
In FMap frame, we've the following actions: |
/** * We actually draw the map in this frame. */ //Show the map container. mapContainerMC._visible = true; //Draw the map salesMap.render(); //Define the listener for back button's click event. var btnListener:Object = new Object(); btnListener.click = function(){ //Re-initialize the map, so that user can plot again //as we're allowing the user to go back to form and then //map the data again. salesMap.remove(); salesMap.reInit(); //Hide the map container so that it doesn't overlap form. mapContainerMC._visible = false; //Goto the form gotoAndStop("FForm"); } //Add event listener btnBack.addEventListener("click",btnListener); stop(); |
In the above code, we're first setting the visibility of map container movie clip (mapContainerMC) to true. If this application is running for the first time, the visibility of this movie clip is already true. However, if the user has viewed the map once, then updated data in form and then clicked "Plot it" button, the container for map is hidden by us. So, we set it to visible again. Thereafter, we straight render the map by calling render() method on salesMap object (which is an instance of WorldMap class). The XML data was already conveyed to map in previous frame. We also define the event listener for "Back" button. In this handler, we're first removing the map using remove() method of this map. This method removes all the items in the map (barring logger). The map still stays in the memory though. remove() method is used when you want to use the same map object again in your movie to plot another map. Since we intend to re-use the map object to plot the data again, we call the remove() method. After the remove() method, we ask the map to re-initialize by calling the reInit() method. This method prepares the map to accept new data and be able to render again. reInit() method should always be called after remove() method of the map, if you intend to use the map object to plot another map. If you need to destroy the map altogether (when you do not need to plot any other maps using the same object), you should call the destroy() method of map. e.g., salesMap.destroy(). This method removes all the associated movie clips, listeners, events and data containers for the map. We also hide the container movie clip for the map, as that's not required in the Form visible state. Finally, we go back to the form state of the application. When you now run the application, you'll get the desired results (as shown above). This example demonstrates all the features that you can attain when using FusionMaps within your Flash movies. |