
Learn the fundamentals of the CustomSWFTool feature in the Yahoo! Maps AS-Flash API for use with ActionScript and the Flash IDE.
Using a CustomSWFTool in your map application allows you to create custom interactions with the map. CustomSWFTool allows you to create a map tool using the same foundation as other built in tools to the Flash API have, such as PanTool.
When using CustomSWFTool and the AS-Flash API, you are also able to use the power of Flash to create a completely custom map, especially when combined with the other powerful tools built into the API, such as CustomSWFMarker.
This tutorial will walk through how to use CustomSWFTool and also recreate the new EdgePan tool available in API 3.04, to show how it was made and how easy it is to make such tools.
To create and test applications using the Yahoo! Maps AS-Flash API, you'll need Macromedia® Flash MX 2004® or higher and a browser with Flash Player 7 or higher. You will also need to download the Yahoo! Maps MXP (Macromedia® eXtensions Package) file that will install the appropriate classes onto your machine. An application ID is required to use the AS-Flash API. You can get an application ID here.
For best results, you should launch your web pages from a web server. However, it's possible to run applications directly from your hard drive by double-clicking on them. If you run them from your hard drive, you may see a warning from the Flash Player that your application is trying to access data from a third-party server (Yahoo!). You can remove this warning by adjusting your Flash Player settings.
Once you have agreed to the terms and downloaded the Yahoo! Maps MXP file, double click on it to launch the Macromedia Extension Manager and automatically install the extension. Please make sure that you have the latest version of the Extension Manager installed. You will need to restart the Flash IDE if you have it open in order for the classes to be available. After the extension is loaded, open the Components panel and expand the Yahoo tree where you should now see com.yahoo.maps.api.flash.YahooMap. To use the component, drag com.yahoo.maps.api.flash.YahooMap from the Components panel onto the stage. Resize the component with the Transform tool or Property inspector and test your movie to see the map. To interact with the map you will need to give the component an instance name. For the examples below, myMap is the instance name.
When adding the Map component to the stage, you should specify what the map will display by default as well as enter your Yahoo! application ID. With the component selected, click on the Parameters tab in the Properties inspector. Here you can specify the starting location, default zoom level, and Yahoo! application ID for your map. The starting location can either be an address, taking advantage of our built-in geocoding, or a latitude and longitude. The zoom level can be any integer from 1 to 17. If no zoom level is specified, the map will default to a zoom level of 14 over the specified location. If no location is specified, the map will default to the center of the United States at the specified zoom level.
To make your map draggable, import the PanTool and then add it to your map. To add the PanTool to the map, wait for the map to initialize (the EVENT_INITIALIZE event) and then use the Map.addTool() method to add the PanTool to the map. The map will use the default zoom level, application ID, and location that you set in the Properties inspector for the map component.
This example uses the demonstration application ID "YD-IuIdb7M_JX_sVWlpbqn_8g--"; you cannot use this Application ID and must request one here.
import com.yahoo.maps.tools.PanTool;
import com.yahoo.maps.markers.CustomSWFMarker;
myMap.addEventListener(
com.yahoo.maps.api.flash.YahooMap.EVENT_INITIALIZE,
onInitMap);
function onInitMap(eventData) {
var panTool = new PanTool();
myMap.addTool(panTool, true);
}
Run the example above Download source
The next step will be to create the tool that will control interaction with the map. Our tool will be 100% ActionScript based, and leaves the door open for your own styling and tweaks.
Built into CustomSWFTool there a number of built in functions that you can use, such as initTool, onToolEnterFrame, onToolMouseDown, onToolMouseUp, onToolMouseMove and detach.
import mx.graphics.Point; var mouseXY:Object;
var myMapSize:Object;
var map; var scrollLeftBuffer:Object; var scrollRightBuffer:Object; var scrollDownBuffer:Object; var scrollUpBuffer:Object; var buffer:Number = 40; //Inside var bufferTwo:Number = 25; //Middle var bufferThree:Number = 15; //Outside var edgePanStop:Number = 3;
function initTool(event:Object) {
map = event.map;
}
function getMapSize(event:Object) {
myMapSize = event;
scrollRightBuffer = {max:myMapSize.w, min:myMapSize.w-buffer};
scrollDownBuffer = {max:myMapSize.h, min:myMapSize.h-buffer};
scrollLeftBuffer = {max:myMapSize.x, min:myMapSize.x+buffer};
scrollUpBuffer = {max:myMapSize.y, min:myMapSize.y+buffer};
}
function onToolMouseMove(event:Object) {
mouseXY = { x:_parent._parent._parent._xmouse,
y:_parent._parent._parent._ymouse }
};
function onToolEnterFrame(event:Object) {
if (mouseXY.x>=scrollRightBuffer.min && mouseXY.x<=scrollRightBuffer.max) {
if (mouseXY.x>=(myMapSize.x+myMapSize.w)-buffer && mouseXY.x<(myMapSize.x+myMapSize.w)-bufferTwo) {
//Inside
var delta:Point = new Point(-20, 0);
map.setCenterByPointDelta(delta);
}
if (mouseXY.x>(myMapSize.x+myMapSize.w)-bufferTwo && mouseXY.x<(myMapSize.x+myMapSize.w)-bufferThree) {
//Middle
var delta:Point = new Point(-35, 0);
map.setCenterByPointDelta(delta);
}
if (mouseXY.x>(myMapSize.x+myMapSize.w)-bufferThree && mouseXY.x<=((myMapSize.x+myMapSize.w)-edgePanStop)) {
//Outside
var delta:Point = new Point(-70, 0);
map.setCenterByPointDelta(delta);
}
}
if (mouseXY.x<=scrollLeftBuffer.min && mouseXY.x>scrollLeftBuffer.max) {
if (mouseXY.x>=(myMapSize.x+edgePanStop) && mouseXY.xmyMapSize.x+bufferThree && mouseXY.xmyMapSize.x+bufferTwo && mouseXY.x<=myMapSize.x+buffer) {
// Inside
var delta:Point = new Point(20, 0);
map.setCenterByPointDelta(delta);
}
}
if (mouseXY.y=scrollUpBuffer.max) {
if (mouseXY.y>=(myMapSize.y+edgePanStop) && mouseXY.ymyMapSize.y+bufferThree && mouseXY.ymyMapSize.y+bufferTwo && mouseXY.y<=myMapSize.y+buffer) {
//Inside
var delta:Point = new Point(0, 20);
map.setCenterByPointDelta(delta);
}
}
if (mouseXY.y>scrollDownBuffer.min && mouseXY.y<=scrollDownBuffer.max) {
if (mouseXY.y<=(myMapSize.y+myMapSize.h)-bufferTwo && mouseXY.y>=(myMapSize.y+myMapSize.h)-buffer) {
// Inside
var delta:Point = new Point(0, -20);
map.setCenterByPointDelta(delta);
}
if (mouseXY.y<(myMapSize.y+myMapSize.h)-bufferThree && mouseXY.y>myMapSize.h-bufferTwo) {
//Middle
var delta:Point = new Point(0, -35);
map.setCenterByPointDelta(delta);
}
if (mouseXY.y<((myMapSize.y+myMapSize.h)-edgePanStop) && mouseXY.y>=(myMapSize.y+myMapSize.h)-bufferThree) {
//Outside
var delta:Point = new Point(0, -70);
map.setCenterByPointDelta(delta);
}
}
}
import com.yahoo.maps.tools.CustomSWFTool; import com.yahoo.maps.tools.PanTool; import com.yahoo.maps.widgets.ToolBarWidget; var toolURL:String = "tool.swf"; var iconURL:String = "icon.swf";
var panTool = new PanTool(); var toolBar = new ToolBarWidget(); var customTool = new CustomSWFTool( toolURL, iconURL );
Note: A CustomSWFTool takes two parameters, the url of the tool swf and the url of the icon swf/png.
myMap.addEventListener(com.yahoo.maps.api.flash.YahooMap.EVENT_INITIALIZE, onInitMap); customTool.addEventListener(com.yahoo.maps.tools.CustomSWFTool.EVENT_LOADED, onToolAdd);
function onInitMap(eventData) {
myMap.addWidget(toolBar);
myMap.addTool(customTool, true);
myMap.addTool(pan);
}
function onToolAdd(eventData) {
tool = eventData.target.mcContainer;
myMapSize = { x:myMap._x,
y:myMap._y,
h:myMap.map.height,
w:myMap.map.width };
tool.getMapSize(myMapSize);
}
The finished code:
Map:
import com.yahoo.maps.tools.CustomSWFTool;
import com.yahoo.maps.tools.PanTool;
import com.yahoo.maps.widgets.ToolBarWidget;
var toolURL:String = "tool.swf";
var iconURL:String = "icon.swf";
var panTool = new PanTool();
var toolBar = new ToolBarWidget();
var customTool = new CustomSWFTool( toolURL, iconURL );
myMap.addEventListener(com.yahoo.maps.api.flash.YahooMap.EVENT_INITIALIZE, onInitMap);
customTool.addEventListener(com.yahoo.maps.tools.CustomSWFTool.EVENT_LOADED, onToolAdd);
function onInitMap(eventData) {
myMap.addWidget(toolBar);
myMap.addTool(customTool, true);
myMap.addTool(pan);
}
function onToolAdd(eventData) {
tool = eventData.target.mcContainer;
myMapSize = { x:myMap._x,
y:myMap._y,
h:myMap.map.height,
w:myMap.map.width };
tool.getMapSize(myMapSize);
}
Tool:
import mx.graphics.Point;
var mouseXY:Object;
var myMapSize:Object;
var map;
var scrollLeftBuffer:Object;
var scrollRightBuffer:Object;
var scrollDownBuffer:Object;
var scrollUpBuffer:Object;
var buffer:Number = 40;
//Inside
var bufferTwo:Number = 25;
//Middle
var bufferThree:Number = 15;
//Outside
var edgePanStop:Number = 3;
function initTool(event:Object) {
map = event.map;
}
function getMapSize(event:Object) {
myMapSize = event;
scrollRightBuffer = {max:myMapSize.w, min:myMapSize.w-buffer};
scrollDownBuffer = {max:myMapSize.h, min:myMapSize.h-buffer};
scrollLeftBuffer = {max:myMapSize.x, min:myMapSize.x+buffer};
scrollUpBuffer = {max:myMapSize.y, min:myMapSize.y+buffer};
}
function onToolMouseMove(event:Object) {
mouseXY = {x:_parent._parent._parent._xmouse, y:_parent._parent._parent._ymouse};
}
function onToolEnterFrame(event:Object) {
if (mouseXY.x>=scrollRightBuffer.min && mouseXY.x<=scrollRightBuffer.max) {
if (mouseXY.x>=(myMapSize.x+myMapSize.w)-buffer && mouseXY.x<(myMapSize.x+myMapSize.w)-bufferTwo) {
//Inside
var delta:Point = new Point(-20, 0);
map.setCenterByPointDelta(delta);
}
if (mouseXY.x>(myMapSize.x+myMapSize.w)-bufferTwo && mouseXY.x<(myMapSize.x+myMapSize.w)-bufferThree) {
//Middle
var delta:Point = new Point(-35, 0);
map.setCenterByPointDelta(delta);
}
if (mouseXY.x>(myMapSize.x+myMapSize.w)-bufferThree && mouseXY.x<=((myMapSize.x+myMapSize.w)-edgePanStop)) {
//Outside
var delta:Point = new Point(-70, 0);
map.setCenterByPointDelta(delta);
}
}
if (mouseXY.x<=scrollLeftBuffer.min && mouseXY.x>scrollLeftBuffer.max) {
if (mouseXY.x>=(myMapSize.x+edgePanStop) && mouseXY.xmyMapSize.x+bufferThree && mouseXY.xmyMapSize.x+bufferTwo && mouseXY.x<=myMapSize.x+buffer) {
// Inside
var delta:Point = new Point(20, 0);
map.setCenterByPointDelta(delta);
}
}
if (mouseXY.y=scrollUpBuffer.max) {
if (mouseXY.y>=(myMapSize.y+edgePanStop) && mouseXY.ymyMapSize.y+bufferThree && mouseXY.ymyMapSize.y+bufferTwo && mouseXY.y<=myMapSize.y+buffer) {
//Inside
var delta:Point = new Point(0, 20);
map.setCenterByPointDelta(delta);
}
}
if (mouseXY.y>scrollDownBuffer.min && mouseXY.y<=scrollDownBuffer.max) {
if (mouseXY.y<=(myMapSize.y+myMapSize.h)-bufferTwo && mouseXY.y>=(myMapSize.y+myMapSize.h)-buffer) {
// Inside
var delta:Point = new Point(0, -20);
map.setCenterByPointDelta(delta);
}
if (mouseXY.y<(myMapSize.y+myMapSize.h)-bufferThree && mouseXY.y>myMapSize.h-bufferTwo) {
//Middle
var delta:Point = new Point(0, -35);
map.setCenterByPointDelta(delta);
}
if (mouseXY.y<((myMapSize.y+myMapSize.h)-edgePanStop) && mouseXY.y>=(myMapSize.y+myMapSize.h)-bufferThree) {
//Outside
var delta:Point = new Point(0, -70);
map.setCenterByPointDelta(delta);
}
}
}
Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings