AFComponents.com

Working with Overlays: Adding a Polygon
Updated: Feb 15, 2008   Views: 2647  
Description: This tutorial will show you how to add a simple styled polygon to the UMap and an event that shows an info window for our polygon on the first roll over.
The Completed Project



Complete Code


import com.afcomponents.umap.overlays.Polygon;
import com.afcomponents.umap.styles.GeometryStyle;
import com.afcomponents.umap.types.LatLng;
import com.afcomponents.umap.events.OverlayEvent;

umap.setCenter(new LatLng(38.873929,-96.899414), 4);

var style:GeometryStyle = new GeometryStyle();
style.fill = "rgb";
style.fillRGB = 0xFF0000;
style.fillAlpha = .5;
style.strokeRGB = 0x000000;
style.strokeAlpha = .6;

var param:Object = new Object();
param.points = new Array();
param.points.push(new LatLng(41.000664,-109.049987));
param.points.push(new LatLng(41.002334,-102.051728));
param.points.push(new LatLng(36.99293,-102.042244));
param.points.push(new LatLng(36.999031,-109.045178));
param.points.push(new LatLng(41.000664,-109.049987));
param.name = "State of Colorado";

var myPoly:Polygon = new Polygon(param, style);
umap.addOverlay(myPoly);

function handleOver(event:Event=null):void {
   event.target.openInfoWindow({position: new LatLng(39,-105.9), title: event.target.name, autoCloseInfo: true});
   myPoly.removeEventListener(OverlayEvent.ROLL_OVER, handleOver);
}
myPoly.addEventListener(OverlayEvent.ROLL_OVER, handleOver);


Setting up the UMap

To begin this tutorial make sure that you have successfully installed the Umap component and dragged an instance onto the stage. Make sure that you give the component an instance name of umap.

Understanding the Plan for ActionScript

After we have our basic map setup, we now want to add a polygon on our UMap that shows the state of colorado on a map of the United States. We will do this using ActionScript. Before we dive into the code, lets understand the basic things we will be doing.
  • Since our polygon will be focused on United States, we want to pan and zoom to that position.
  • We also want a styled polygon so we create our GeometryStyle object.
  • We also need to add all our points in a parameters object
  • We then create our polygon based on parameters object with our points, and style with our GeometryStyle object.
  • Lastly, we want to have an event listener for when the user rolls over that displays an info window with the title for our polygon.
Pan and Zoom to Our Location

Our first thing that we will do is pan and zoom to the United States because that is what we want the map to focus on.

import com.afcomponents.umap.types.LatLng;

umap.setCenter(new LatLng(38.873929,-96.899414),4);


Create a Style for Our Polygon

We don't want just a default polygon, maybe one with some color and alpha. So what we can do is create a style object that we can apply to the polygons we will create. For more things you can change for the polygon take a look at the API Documentation in the com.afcomponents.umap.styles.GeometryStyle class.

import com.afcomponents.umap.styles.GeometryStyle;

var style:GeometryStyle = new GeometryStyle();
style.fill = "rgb";
style.fillRGB = 0xFF0000;
style.fillAlpha = .5;
style.strokeRGB = 0x000000;
style.strokeAlpha = .6;


Note: we will apply this to our polygon when we add it. 

Creating Our Parameters Objects that Includes Points for Our Polygon

Our polygon needs a few points to connect for our polygon drawn from. We will do this by creating a paramters object with a points array inside of it. We will also give our polygon a title that will show up in our info window.

import com.afcomponents.umap.types.LatLng;

var param:Object = new Object();
param.points = new Array();
param.points.push(new LatLng(41.000664,-109.049987));
param.points.push(new LatLng(41.002334,-102.051728));
param.points.push(new LatLng(36.99293,-102.042244));
param.points.push(new LatLng(36.999031,-109.045178));
param.points.push(new LatLng(41.000664,-109.049987));
param.name = "State of Colorado";


Add Our Polygon to UMap

Next we need to create our polygon with our parameters object and our style object. After we have created it, we add our polygon as an overlay to our map.

import com.afcomponents.umap.overlays.Polygon;

var myPoly:Polygon = new Polygon(param, style);
umap.addOverlay(myPoly);


Add an Event Listener to Our Polygon

Next, we are going to add an event listener to our polygon so that it shows an info window that displays our title for polygon the first time we roll over. We will just have it open at one our middle points. We will also remove the event listener inside the function so that it is only called once.

import com.afcomponents.umap.events.OverlayEvent;

function handleOver(event:Event=null):void {
   event.target.openInfoWindow({position: new LatLng(39,-105.9), title: event.target.name, autoCloseInfo: true});
   myPoly.removeEventListener(OverlayEvent.ROLL_OVER, handleOver);
}
myPoly.addEventListener(OverlayEvent.ROLL_OVER, handleOver);


© 2005-2007 advanced flash components