Flex And Yahoo Maps
| The application we are going to build here is going to be a very simple local search example. Ok, play around with it below. It is initialized to search New York for "italian". The application is built using just a couple flex components. You can view the source here. |
Grabbing the Component
The first item of business is going and getting the Yahoo Map component and installing the swc file. This is fairly easy to do. You can get the component from Yahoo here. Once you have downloaded the component, you need to get a application api key. You can get this here, fill out the information and you will get your api key. This will be used to create the map component. To install the swc file following the below directions, which are taken from the Yahoo Map page.- Find your project in the Flex Navigator.
- Right-click on the project and choose Properties or open the Project menu and choose Properties.
- In the project properties dialog, choose Flex Build Path.
- Select the Library path tab, and press the Add SWC... button.
- Enter the location of the component SWC. It should be in
[download location]/Build/YahooMap.swc. - Press OK in the Add SWC dialog.
- Press OK to close the project properties.
- The YahooMap component is now available in your project.
Basic Interface
Once we have our component added to our application we can get started building the example. The example has a very simple interface that we are going to build. It has the basic application with a couple parameters. The major changes to the application are the layout which we change tovertical and got rid of all the padding. Inside the application we have two big containers - one is a HBox and one is an UIComponent.The
HBox will hold the simple set of controls for local search. We have a label, text input, and button in the box for inputting the search terms and searching. The second UIComponent is actually for adding the Yahoo Map component. This may sound a little bit different than normal, but we need a component that can hold a Sprite as a child (YahooMap component extends Sprite). This means Container components are out. Yahoo probably went this route to keep the component as small as possible.<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="500" height="550"
layout="vertical" verticalGap="0"
paddingBottom="0" paddingTop="0"
paddingLeft="0" paddingRight="0">
<mx:HBox width="100%" horizontalAlign="left"
paddingBottom="2" paddingTop="2">
<mx:Label text="Local Search" />
<mx:TextInput id="searchTerm" width="200" text="italian" />
<mx:Button label="Search" />
</mx:HBox>
<mx:UIComponent id="mapContainer" width="100%" height="100%"/>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="500" height="550"
layout="vertical" verticalGap="0"
paddingBottom="0" paddingTop="0"
paddingLeft="0" paddingRight="0">
<mx:HBox width="100%" horizontalAlign="left"
paddingBottom="2" paddingTop="2">
<mx:Label text="Local Search" />
<mx:TextInput id="searchTerm" width="200" text="italian" />
<mx:Button label="Search" />
</mx:HBox>
<mx:UIComponent id="mapContainer" width="100%" height="100%"/>
</mx:Application>
Adding Yahoo Map Component
Next we will add the map component to our application. To do this we are going to add a little bit of Script to our application. We start with adding an event to ourApplication for creation complete. This event will call initApp which initializes the Yahoo Map component. The following code shows our new Application tag.<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="500" height="550"
layout="vertical" verticalGap="0"
paddingBottom="0" paddingTop="0"
paddingLeft="0" paddingRight="0"
creationComplete="initApp()">
width="500" height="550"
layout="vertical" verticalGap="0"
paddingBottom="0" paddingTop="0"
paddingLeft="0" paddingRight="0"
creationComplete="initApp()">
Now we add a
Script tag for the all the Actionscript code. We first add a private variable that is a YahooMap called ymap. Inside our script tag we add a function for initApp. Our function starts off by setting ymap equal to a new YahooMap. The constructor for the Yahoo Map takes four items: application id (what we registered for earlier), width, height, and optional local (defaults to "en"). Once we have created the map we do a couple items. First we add an event listener to listen for when the map has been initialized; this will call the function initializeMap. We tell the map to include a couple pieces which include the zoom buttons, types ("Map", "Satellite", and "Hybrid"), and panning - click and drag. The last thing to do is add the map to our map container (the UIComponent we added earlier). We can see this code below.<mx:Script>
<![CDATA[
import com.yahoo.maps.api.YahooMapEvent;
import com.yahoo.maps.api.YahooMap;
private var ymap:YahooMap;
private function initApp():void
{
this.ymap = new YahooMap("appid", this.width, this.height);
this.ymap.addEventListener(
YahooMapEvent.MAP_INITIALIZE, initializeMap);
this.ymap.addPanControl();
this.ymap.addZoomWidget();
this.ymap.addTypeWidget();
this.mapContainer.addChild(this.ymap);
}
]]>
</mx:Script>
<![CDATA[
import com.yahoo.maps.api.YahooMapEvent;
import com.yahoo.maps.api.YahooMap;
private var ymap:YahooMap;
private function initApp():void
{
this.ymap = new YahooMap("appid", this.width, this.height);
this.ymap.addEventListener(
YahooMapEvent.MAP_INITIALIZE, initializeMap);
this.ymap.addPanControl();
this.ymap.addZoomWidget();
this.ymap.addTypeWidget();
this.mapContainer.addChild(this.ymap);
}
]]>
</mx:Script>
To finish up the initialization of the Map we need to add the
initializeMap function to our script. We put this right below initApp. During this function we do two thing initially, set the initial zoom and the initial location. For the zoom I set it to 3, which is a city close up, and also set a center point to the latitude and longitude of New York city. You can examine this code below.private function initializeMap(event:YahooMapEvent):void
{
this.ymap.zoomLevel = 4;
this.ymap.centerLatLon = new LatLon(40.714, -74.006);
}
{
this.ymap.zoomLevel = 4;
this.ymap.centerLatLon = new LatLon(40.714, -74.006);
}
That is all that is takes to get a map to show up. Pretty simple ehh?
Adding Local Search
To make this example even better we are going to show how to build in very simple local search functionality. The first thing we are going to do is add two events to our interface. The first event we are going to add is for theTextInput - to which we are going to add a keydown event listener. For our Button component we will add a click event listener. Code for the text input and button follows.<mx:TextInput id="searchTerm" width="200" text="italian"
keyDown="{checkEnterKey(event)}" />
<mx:Button label="Search" click="{doLocalSearch()}" />
keyDown="{checkEnterKey(event)}" />
<mx:Button label="Search" click="{doLocalSearch()}" />
Next we add a function for the button click to actually do the local search. First we need to add another private variable of type
LocalSearch. So we add a function to our script which will use the Yahoo API to do a local search. This function, named doLocalSearch, will first create a new LocalSearch, then add an event listener for when the search completes, and finally do the actual search. This code is below.private function doLocalSearch():void
{
this.localSearch = new LocalSearch();
this.localSearch.addEventListener(
LocalSearchEvent.SEARCH_SUCCESS, handleSearchResults);
this.localSearch.searchLocal
(
this.searchTerm.text,
this.ymap.zoomLevel,
this.ymap.centerLatLon
);
}
{
this.localSearch = new LocalSearch();
this.localSearch.addEventListener(
LocalSearchEvent.SEARCH_SUCCESS, handleSearchResults);
this.localSearch.searchLocal
(
this.searchTerm.text,
this.ymap.zoomLevel,
this.ymap.centerLatLon
);
}
Now we need to handle the results that we get back from Yahoo. This is done in the function, named
handleSearchResults, spelled out below. We first remove all the Markers on the map; next we grab the results from the event and put this info into an array. Lastly we loop through these results and add each one to the markers map individually.private function handleSearchResults(event:LocalSearchEvent)
:void
{
this.ymap.markerManager.removeAllMarkers();
var srcResults:LocalSearchResults
= event.data as LocalSearchResults;
var results:Array = srcResults.results;
for(var i:int = 0; i <results.length; i++)
{
var item:LocalSearchItem = results[i];
var marker:SearchMarker = new SearchMarker(item);
this.ymap.markerManager.addMarker(marker);
}
}
:void
{
this.ymap.markerManager.removeAllMarkers();
var srcResults:LocalSearchResults
= event.data as LocalSearchResults;
var results:Array = srcResults.results;
for(var i:int = 0; i <results.length; i++)
{
var item:LocalSearchItem = results[i];
var marker:SearchMarker = new SearchMarker(item);
this.ymap.markerManager.addMarker(marker);
}
}
And this gives us a working search. To make this slightly more complete we are going to do two things. First we add a quick function call to our
initializeMap function which initializes the map to have some markers on it. Finally we add the function for the keyDown event for our text input. This function checks to see if the key pressed was the "Enter" key and if so then simply calls the local search function. Both pieces of code are below.private function initializeMap(event:YahooMapEvent):void
{
this.ymap.zoomLevel = 4;
this.ymap.centerLatLon = new LatLon(40.714, -74.006);
this.doLocalSearch();
}
{
this.ymap.zoomLevel = 4;
this.ymap.centerLatLon = new LatLon(40.714, -74.006);
this.doLocalSearch();
}
The key down code.
private function checkEnterKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.ENTER)
{
doLocalSearch();
}
}
{
if(event.keyCode == Keyboard.ENTER)
{
doLocalSearch();
}
}
Complete Code
This completes the entire tutorial; leaving us with the complete code below.<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="500" height="550"
layout="vertical" verticalGap="0"
paddingBottom="0" paddingTop="0"
paddingLeft="0" paddingRight="0"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
import com.yahoo.maps.api.markers.SearchMarker;
import com.yahoo.maps.webservices.local.LocalSearchItem;
import com.yahoo.maps.webservices.local.LocalSearchResults;
import
com.yahoo.maps.webservices.local.events.LocalSearchEvent;
import com.yahoo.maps.webservices.local.LocalSearch;
import com.yahoo.maps.api.core.location.LatLon;
import com.yahoo.maps.api.YahooMapEvent;
import com.yahoo.maps.api.YahooMap;
private var ymap:YahooMap;
private var localSearch:LocalSearch;
private function initApp():void
{
this.ymap = new YahooMap("appid", this.width, this.height);
this.ymap.addEventListener(
YahooMapEvent.MAP_INITIALIZE, initializeMap);
this.ymap.addPanControl();
this.ymap.addZoomWidget();
this.ymap.addTypeWidget();
this.mapContainer.addChild(this.ymap);
}
private function initializeMap(event:YahooMapEvent):void
{
this.ymap.zoomLevel = 4;
this.ymap.centerLatLon = new LatLon(40.714, -74.006);
this.doLocalSearch();
}
private function checkEnterKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.ENTER)
{
doLocalSearch();
}
}
private function doLocalSearch():void
{
this.localSearch = new LocalSearch();
this.localSearch.addEventListener(
LocalSearchEvent.SEARCH_SUCCESS, handleSearchResults);
this.localSearch.searchLocal
(
this.searchTerm.text,
this.ymap.zoomLevel,
this.ymap.centerLatLon
);
}
private function handleSearchResults(event:LocalSearchEvent)
:void
{
this.ymap.markerManager.removeAllMarkers();
var srcResults:LocalSearchResults
= event.data as LocalSearchResults;
var results:Array = srcResults.results;
for(var i:int = 0; i <results.length; i++)
{
var item:LocalSearchItem = results[i];
var marker:SearchMarker = new SearchMarker(item);
this.ymap.markerManager.addMarker(marker);
}
}
]]>
</mx:Script>
<mx:HBox width="100%" horizontalAlign="left"
paddingBottom="2" paddingTop="2">
<mx:Label text="Local Search" />
<mx:TextInput id="searchTerm" width="200" text="italian"
keyDown="{checkEnterKey(event)}" />
<mx:Button label="Search" click="{doLocalSearch()}" />
</mx:HBox>
<mx:UIComponent id="mapContainer" width="100%" height="100%"/>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="500" height="550"
layout="vertical" verticalGap="0"
paddingBottom="0" paddingTop="0"
paddingLeft="0" paddingRight="0"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
import com.yahoo.maps.api.markers.SearchMarker;
import com.yahoo.maps.webservices.local.LocalSearchItem;
import com.yahoo.maps.webservices.local.LocalSearchResults;
import
com.yahoo.maps.webservices.local.events.LocalSearchEvent;
import com.yahoo.maps.webservices.local.LocalSearch;
import com.yahoo.maps.api.core.location.LatLon;
import com.yahoo.maps.api.YahooMapEvent;
import com.yahoo.maps.api.YahooMap;
private var ymap:YahooMap;
private var localSearch:LocalSearch;
private function initApp():void
{
this.ymap = new YahooMap("appid", this.width, this.height);
this.ymap.addEventListener(
YahooMapEvent.MAP_INITIALIZE, initializeMap);
this.ymap.addPanControl();
this.ymap.addZoomWidget();
this.ymap.addTypeWidget();
this.mapContainer.addChild(this.ymap);
}
private function initializeMap(event:YahooMapEvent):void
{
this.ymap.zoomLevel = 4;
this.ymap.centerLatLon = new LatLon(40.714, -74.006);
this.doLocalSearch();
}
private function checkEnterKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.ENTER)
{
doLocalSearch();
}
}
private function doLocalSearch():void
{
this.localSearch = new LocalSearch();
this.localSearch.addEventListener(
LocalSearchEvent.SEARCH_SUCCESS, handleSearchResults);
this.localSearch.searchLocal
(
this.searchTerm.text,
this.ymap.zoomLevel,
this.ymap.centerLatLon
);
}
private function handleSearchResults(event:LocalSearchEvent)
:void
{
this.ymap.markerManager.removeAllMarkers();
var srcResults:LocalSearchResults
= event.data as LocalSearchResults;
var results:Array = srcResults.results;
for(var i:int = 0; i <results.length; i++)
{
var item:LocalSearchItem = results[i];
var marker:SearchMarker = new SearchMarker(item);
this.ymap.markerManager.addMarker(marker);
}
}
]]>
</mx:Script>
<mx:HBox width="100%" horizontalAlign="left"
paddingBottom="2" paddingTop="2">
<mx:Label text="Local Search" />
<mx:TextInput id="searchTerm" width="200" text="italian"
keyDown="{checkEnterKey(event)}" />
<mx:Button label="Search" click="{doLocalSearch()}" />
</mx:HBox>
<mx:UIComponent id="mapContainer" width="100%" height="100%"/>
</mx:Application>
Well I hope this was a start back towards more tutorials. If you have question about this tutorial or anything related please feel free to leave a comment. Also let us know if you would like to see more Yahoo Maps API tutorials.

February 15th, 2008 at 11:08 am
I am trying to grab the address from a searchmarker, and there seems to be no way to trigger a click event on the marker. Do you have any suggestions? Thanks!
February 15th, 2008 at 11:48 am
Well it looks like you can attach an event listener to the search marker for the click event. Then in the listening function you can use the current target of the click to get to the marker info. This includes the latitude and longitude information.
Some code below should help explain this.
:void
{
this.ymap.markerManager.removeAllMarkers();
var srcResults:LocalSearchResults
= event.data as LocalSearchResults;
var results:Array = srcResults.results;
for(var i:int = 0; i <results.length; i++)
{
var item:LocalSearchItem = results[i];
var marker:SearchMarker = new SearchMarker(item);
marker.addEventListener(MouseEvent.CLICK,
markerClicked);
this.ymap.markerManager.addMarker(marker);
}
}
private function markerClicked(event:Event):void
{
trace(SearchMarker(event.currentTarget).latlon.toString());
}
February 15th, 2008 at 1:49 pm
This works perfectly! Thank you for the help.
March 2nd, 2008 at 9:44 pm
checkEnterKey() isn’t neccesary. has the same effect. I was doing the same thing until this afternoon.
I also had a question. I’m trying to make a tabbed interface with a map, and it won’t show up, or fire the YahooMapEvent.MAP_INITIALIZE event after the first map is displayed. Very frustrating.
March 2nd, 2008 at 9:45 pm
This got stripped out.
March 18th, 2008 at 9:54 pm
Has anyone tried to implement the “Callout Component” (http://www.adobe.com/devnet/flex/samples/fig_callout/) in the new Yahoo Maps Flex 3 component? I’m trying to add these kind of markers into the map instead of the SimpleMarker… Any ideas where to start?
April 6th, 2008 at 3:21 pm
is it really possible to create a yahoo map application, where i can draw marks on the map so that users see places i underline?
June 11th, 2008 at 10:21 am
Nice and clear!
Your download link happen to point to YahooMaps.swc in release 0.9.0.
We may use newer realease from: http://developer.yahoo.com/flash/maps/.
Just be carefull not to use release 0.9.2 for the markers wont popup related info…