Using The Yahoo! Maps ActionScript 3 API To Create A Map With Local Places - A Beginner's Tutorial

I was excited to learn that Yahoo! released an ActionScript 3.0 API for their maps service. I've just started to play with the API, but it looks relatively easy to use. I do wish there was a bit more developer's documentation. Since even after studying the examples provided by Yahoo!, I was confused on how to create a map and display places on the map (especially the results of doing a search on Yahoo! Local), I thought a beginner's guide might be useful for others.

I've created a simple Flex application demonstrating how to create a map, place a SimpleMarker object on the map, and display the results of doing a search of the Yahoo! Local listings. You can view the application and get its source code (right click on the application) here. I tried to include some good comments in the source code to help you follow what I did. Here are the key steps:

Be sure to follow the directions at http://developer.yahoo.com/flash/maps/index.html on how to add to your project's library the Yahoo! maps component SWC. You'll know you've done that correctly if you can type inside the mx:Script tag "import com." and auto complete lists yahoo as one the choices. On the same page as these instructions is a link to get a Yahoo! developer API key. When you create a Yahoo! map in your Flex application you'll have to supply your API key.

After you have created a YahooMap object, you can manipulate that object using all the methods listed here. In my example, I created an Address object and then called the Address class's geocode method to get the latitude and longitude for my Address object. Then I centered the map on that latitude and longitude.

I then created a SimpleMarker object and assigned its latlon (latitude-longitude) property to the latlon returned by calling the geocode method on the Address object. I then used the YahooMap class's markerManager property to add this marker to the map display.

To do a search of Yahoo! Local listings, I created a LocalSearch object and called its searchLocal method. I passed to this method the type of place I wanted to search for, my maps zoom level, and the latlon I wanted to search around.

If the search is successful, you get back a LocalSearchEvent object which contains an array of LocalSearchItem objects. I used each LocalSearchItem object to create a SearchMarker object. I then displayed the SearchMarker object on the map by again using the YahooMap class's markerManager property.

My next goal is to learn how to create my own Marker objects, with my own properties (for example a title) and place those on the map.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
It is probably just my machine, but could you link your source directly from this page. Right clicking and choosing View Source doesn't do anything for me in Firefox or Safari.
# Posted By jamie | 3/11/08 3:45 AM
Jamie - try right clicking on a part of the application that is not the map. Apparently, right clicking on the map doesn't work.
# Posted By Bruce | 3/11/08 4:29 AM
Ahh.. Don't right click on the map, get the title at the top of the map and it will show the source.
# Posted By jamie | 3/11/08 5:11 AM
Bruce,

I seem to be getting an error with this line:

_yahooMap = new YahooMap(APP_ID, mapContainer.width, mapContainer.height);

I get the 1137:Incorrect number of arguments. Expected no more than 0.

What am I doing wrong?
# Posted By David Green | 5/13/08 9:34 AM
hi am trying to follow your example to embedded a map of Kampala city (coordinates "0.19 35.25") but am getting the following compile error on this line

_yahooMap = new YahooMap(APP_ID, mapContainer.width, mapContainer.height);

I get the 1137:Incorrect number of arguments. Expected no more than 0. please help? and when i remove the attributes in the brackets i get the following runtime error

TypeError: Error #1009: Cannot access a property or method of a null object reference.
   at com.yahoo.maps.api.controls::PanControl/::reset()
   at com.yahoo.maps.api.controls::PanControl/set panEnabled()
   at com.yahoo.maps.api::YahooMap/addPanControl()
   at components::contacts/createMap()
   at components::contacts/___Canvas1_creationComplete()
   at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
   at flash.events::EventDispatcher/dispatchEvent()
   at mx.core::UIComponent/dispatchEvent()
   at mx.core::UIComponent/set initialized()
   at mx.managers::LayoutManager/::doPhasedInstantiation()
   at mx.managers::LayoutManager/validateNow()
   at mx.effects::Tween$/::timerHandler()
   at flash.utils::Timer/flash.utils:Timer::_timerDispatch()
   at flash.utils::Timer/flash.utils:Timer::tick()
# Posted By Arthurnasius Amanyire | 5/16/08 6:07 AM
This is the error that I was speaking of Bruce. I copied the source code exactly and it throws the above error.
# Posted By David Green | 5/19/08 7:23 AM
The above mistake is conflating the creation of the map object and initializing it. Simply create the new map(), and then run _yahooMap.int

It will work.
# Posted By Larry Larson | 6/1/08 12:47 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner