Advanced Flash Components
Search!
Search!
Home >  Tutorials >  UMap (AS 3.0) >  Getting Started: Adding Markers...
Getting Started: Adding Markers to UMap
Updated: Feb 11, 2008   Views: 1618  
Description: This tutorial will show you how to add simple markers to the UMap, a brief touch on styling the markers, and how to add an event listener to the your markers.
The Completed Project



Complete Code


import com.afcomponents.umap.types.LatLng;
import com.afcomponents.umap.overlays.Marker;
import com.afcomponents.umap.styles.MarkerStyle;
import com.afcomponents.umap.events.OverlayEvent;

var myMarker:Marker = new Marker();
myMarker.position = new LatLng (39.75, -105);
myMarker.index = "A";
myMarker.name = "Hello";
myMarker.description = "World";
umap.addOverlay(myMarker);

var myOtherMarker:Marker = new Marker({position: new LatLng (41.38,-2.12), index:"B", name: "I am Legend", description:"I am Marker"});
umap.addOverlay(myOtherMarker);

var myStyle:MarkerStyle = new MarkerStyle();
myStyle.fill = "rgb";
myStyle.fillRGB = 0x3399ff;
myStyle.fillAlpha = 0.9;
myStyle.strokeRGB = 0x000000;
myStyle.strokeAlpha = 1.0;

myMarker.setStyle(myStyle);
myOtherMarker.setStyle(myStyle);

myMarker.addEventListener(OverlayEvent.ROLL_OVER, handleMarkerOver);
myOtherMarker.addEventListener(OverlayEvent.ROLL_OVER, handleMarkerOver);

//Handle Map Marker Over
function handleMarkerOver(event:Event=null):void {
    event.target.openInfoWindow();
}


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 some markers to our map. We will do this using ActionScript. Before we dive into the code, lets understand the basic things we will be doing.
  • Adding a marker with defaults and then setting this marker properties individually
  • Adding a marker by passing parameters inside of new marker method
  • Simple Styling of our Markers
  • Create an event listener for our markers for roll over in which we will open the info window for our markers
Adding a Marker with Defaults and Changing these Properties

Our first thing that will do is create a new marker. We do this by importing a few classes and then creating our new marker object and then adding this to our map.

import com.afcomponents.umap.overlays.Marker;

var myMarker:Marker = new Marker();
umap.addOverlay(myMarker);


This marker will however will just get set to the default properties of the marker and we want our marker at a certain position, a label on it, and different information in its information window. So using the API Documentation in the com.afcomponents.umap.overlays.Marker class we find that this marker has properties that we can set.

import com.afcomponents.umap.types.LatLng;

myMarker.position = new LatLng (39.75, -105);
myMarker.index = "A";
myMarker.name = "Hello";
myMarker.description = "World";


Adding a Marker and Passing Properties Inside of Mew Marker Method

This is similar to the last step and are basically doing the exact same thing of adding our point and setting properties of this point, but this is a way of saving lines of code. Instead of creating a point with defaults and then setting them we are going to set these properties as we create the marker.

import com.afcomponents.umap.overlays.Marker;
import com.afcomponents.umap.types.LatLng;

var myOtherMarker:Marker = new Marker({position: new LatLng (41.38,-2.12), index:"B", name: "I am Legend", description:"I am Marker"});
umap.addOverlay(myOtherMarker);


Styling of Our Markers

Another thing we can do is style our markers so that they look different. Looking at the API Documentation, we find the com.afcomponents.umap.styles.MarkerStyle class and there is a list of the different properties of our marker that we can style. We will create a new MarkerStyle object with defaults and the set a few of the properties.

import com.afcomponents.umap.styles.MarkerStyle;
var myStyle:MarkerStyle = new MarkerStyle();
myStyle.fill = "rgb";
myStyle.fillRGB = 0x3399ff;
myStyle.fillAlpha = 0.9;
myStyle.strokeRGB = 0x000000;
myStyle.strokeAlpha = 1.0;


Lastly, we just need to apply this MarkerStyle to our markers.

myMarker.setStyle(myStyle);
myOtherMarker.setStyle(myStyle);


Adding Roll Over Event to Our Markers

The last thing that we will cover is how to add an event listener to our markers. We will create an event listener for when the user rolls over the marker and this event listener will call a function that opens the information window for targeted marker.

import com.afcomponents.umap.events.OverlayEvent;

myMarker.addEventListener(OverlayEvent.ROLL_OVER, handleMarkerOver);
myOtherMarker.addEventListener(OverlayEvent.ROLL_OVER, handleMarkerOver);

function handleMarkerOver(event:Event=null):void {
    event.target.openInfoWindow();
}