Advanced Flash Components
Search!
Search!
Home >  Tutorials >  UMap (AS 3.0) >  Working with Overlays: Adding a...
Working with Overlays: Adding a Polyline
Updated: Feb 15, 2008   Views: 2078  
Description: This tutorial will show you how to add a simple styled polyline to the UMap and an event that shows an info window for our line on the first roll over.
The Completed Project



Complete Code


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

umap.setCenter(new LatLng(47,6),4);

var style:GeometryStyle = new GeometryStyle();
style.stroke = GeometryStyle.RGB;
style.strokeRGB = 0xFF0000;
style.strokeAlpha = .5;
style.strokeThickness = 10;

var param:Object = new Object();
param.points = new Array();
param.points.push(new LatLng(38.42,-9.10));
param.points.push(new LatLng(40.25,-3.45));
param.points.push(new LatLng(51.36,.05));
param.points.push(new LatLng(48.5,2.2));
param.points.push(new LatLng(41.54,12.29));
param.points.push(new LatLng(52.30,13.25));
param.name = "My Europe Trip";

var myLine:Polyline = new Polyline(param, style);
umap.addOverlay(myLine);

function handleOver(event:Event=null):void {
   event.target.openInfoWindow({position: new LatLng(48.5,2.2), title: event.target.name, autoCloseInfo: true});
   myLine.removeEventListener(OverlayEvent.ROLL_OVER, handleOver);
}
myLine.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 polyline on our UMap that shows where we went our trip to Europe. We will do this using ActionScript. Before we dive into the code, lets understand the basic things we will be doing.
  • Since our line will be focused on Europe, we want to pan and zoom to that position.
  • We also want a style lined so we create our GeometryStyle object.
  • We also need to add all our points in a parameters object
  • We then create our polyline 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 line.
Pan and Zoom to Our Location

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

import com.afcomponents.umap.types.LatLng;

umap.setCenter(new LatLng(47,6),4);


Create a Style for Our Polyline

We don't want just a default line, maybe one with some color, a little thicker, and alpha. So what we can do is create a style object that we can apply to the line or lines we will create. For more things you can change for the line 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.stroke = GeometryStyle.RGB;
style.strokeRGB = 0xFF0000;
style.strokeAlpha = .5;
style.strokeThickness = 10;


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

Creating Our Parameters Objects that Includes Points for Our Polyline

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

var param:Object = new Object();
param.points = new Array();
param.points.push(new LatLng(38.42,-9.10));
param.points.push(new LatLng(40.25,-3.45));
param.points.push(new LatLng(51.36,.05));
param.points.push(new LatLng(48.5,2.2));
param.points.push(new LatLng(41.54,12.29));
param.points.push(new LatLng(52.30,13.25));
param.name = "My Europe Trip";


Add Our Polyline to UMap

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

var myLine:Polyline = new Polyline(param, style);
umap.addOverlay(myLine);


Add an Event Listener to Our Line

Next, we are going to add an event listener to our line so that it shows an info window that displays our title for line 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.

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