Using The New Yahoo! Maps ActionScript 3 API - Creating A Custom Marker

At the end of my previous post on the new Yahoo! maps AS3 API, I mentioned that I wanted to learn how to create my own custom Marker class so that I could display my own graphic and text on the map. After some playing around, I figured out how to create a custom Marker class. You can view an example Flex application here (right click to get the source code).

The key to understanding how to create your own Markers is two-fold. First you can extend the Marker class provided by the Yahoo! maps API. Second, you should understand that the Marker class extends the flash Sprite class. The Sprite class can be used as the base building block for creating user interface components (like a Marker).

I had not used the Sprite class much since I've not had a need to create my own unique user interface component. So I had some learning to do. You can check out the examples in the Flex 2.0.1 language reference and the information about display programming in the Programming ActionScript 3.0 guide. I fortunately have the book Essential ActionScript 3.0 by Colin Mock which has good information about using class Sprite, building shapes using class Graphic, and adding text to custom designed user interface objects.

In my CustomMarker class I added a private _title property of type TextField. Class TextField is an ActionScript 3 class you can use for text display and input. I have a public set method that can be called to set the text property of the TextField object.

In my Flex application, I just needed to import my CustomMarker class. After that I could use the CustomMarker object just like I had used the Marker object in my previous example. Since my CustomMarker class had a public set title method, I could do the following to set the title (which is the text that appears in the TextField object):

_marker.title = theAddress;

It appears that the upper left corner of my CustomMarker is the location on the map for my Address object's latitude and longitude. So as I get better at designing my own custom user interface components I'll have to add some visual clue so that user will know that my CustomMarker is referencing this specific spot on the map (like the tail in the base Marker class).

Update: I created another version of my CustomMarker class that uses a photo as part of the Marker. See that version here (right click on the app to view the source code). You could also create an image using Adobe Fireworks or other program and use that image as part of your custom Marker class.

 

Update: I created another version of my CustomMarker class that lets the user click on the CustomMarker to get more information. See that version here (right click on the app to view the source code). When you create an object of the CustomMarkerURL class, you just need to add an event listener for the click event and have a function to handle that event. Since the Marker class inherits from class Sprite, it inherits the mouse events.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Quick question about code:

Why do you have a [Bindable] theAddress variable (rather than just using the private _address variable)?


It's great following along with your Yahoo Maps series. I don't have time to play around with it right now, but I definitely have the time to follow along your journey :)
# Posted By Gareth | 2/13/08 6:32 AM
Gareth - you're right about just using _address.address instead of a separate theAddress variable. Thanks for pointing that out.
# Posted By Bruce | 2/13/08 8:37 AM
No problem. Wasn't trying to be picky or anything, but thought I might be missing something :)
# Posted By Gareth | 2/13/08 8:56 AM
Hi, great example. But I'm trying to convert my AS2 map to AS3 but what happened to the SWFMarker? How can we create a custom (animated) marker with nice effects etc..? On your sprite I can't even add a button or a datagrid in flex
# Posted By Fréderic Cox | 2/13/08 8:09 PM
man i kind of miss the old customPOIMarker. Is there an equivalent in the new AS3 version somewhere? I just want the standard map marker that displays a name and shows other business info when clicked on. I was hoping you could use the SearchMarker but it looks like all the properties are read-only?
# Posted By Dan | 2/13/08 8:14 PM
I meant the properties of the LocalSearchItem Object (required to add a SearchMarker) are all read-only.
# Posted By Dan | 2/13/08 8:21 PM
Frederic - You can create a SWF and use it as the Marker. See Jesse Warden's blog at http://www.jessewarden.com/

Dan - I'm also trying to figure out how to create my own LocalSearchItem object for use in creating a SearchMarker. I'm hoping we don't have to go to all the trouble of creating a our own custom Marker with all the properties that SearchMarker already has.

In my experiments so far I was able to create a LocalSearchItem object by first creating an XML object with nodes that match the properties of the LocalSearchItem (eg <addr>, <city>, <phone>, etc). But I cannot figure out how to create the LatLon object as part of this XML object (all the other properties are String type). If I can figure that part out I can create my own LocalSearchItem and then use that to create the SearchMarker.

Bruce
# Posted By Bruce | 2/14/08 6:48 AM
Hey bruce you've probably already tried this but i just sniffed the xml being sent for a normal yahoo local result and it looks like they just use simple <lat> and <lon> xml objects. Maybe the LatLon property isn't something you set explicity, it just grabs information from the lat and lon objects and combines them?

example listing returned from yahoo local:

<listing>

<id>21382504</id>

<title><![CDATA[North Beach <b>Pizza</b>]]></title>

<addr><![CDATA[1310 Grant Ave]]></addr>

<city>San Francisco</city>

<state>CA</state>

<zip>94133</zip>

<crossstreet><![CDATA[Near the intersection of Grant Ave and Vallejo St]]></crossstreet>

<phone>(415) 433-2444</phone>

<dphone>4154332444</dphone>

<ycats><data><ycat>96926243</ycat><name><![CDATA[Pizza]]></name></data><data><ycat>96926190</ycat><name

><![CDATA[Italian Restaurants]]></name></data><data><ycat>96926234</ycat><name><![CDATA[Carry Out & Take

Out]]></name></data><data><ycat>96926236</ycat><name><![CDATA[Restaurants]]></name></data></ycats><desc

><![CDATA[]]></desc>

<rating>3.5</rating>

<nrating>127</nrating>

<nreview>54</nreview>

<accountid>-2147483648</accountid>

<venueid></venueid>

<eventutime></eventutime>

<eventdatetime></eventdatetime>

<lat>37.798984</lat>

<lon>-122.407059</lon>

<detailurl><![CDATA[http://local.yahoo.com/details?id=21382504&stx...

]]></detailurl>

<website><![CDATA[http://www.northbeachpizza.com/]]></website>

<readreviewsurl><![CDATA[http://local.yahoo.com/details?id=21382504#reviews...]]></readreviewsurl>

<reviewurl><![CDATA[http://local.yahoo.com/reviews?id=21382504]]></reviewurl>

<eid><![CDATA[INFOUSA=457969962|AMACAI=67311952|BIZSUBMIT_FREE=1135737|BIZSUBMIT_FREE=DIN_1135737|CHEFMOZ

=North_Beach_Pizza,_Too954331796|CHEFMOZ=DIN_North_Beach_Pizza,_Too954331796|RR=436387|RR=DIN_436387

|INFOUSA|AMACAI|BIZSUBMIT_FREE|BIZSUBMIT_FREE|CHEFMOZ|CHEFMOZ|RR|RR]]></eid>

</listing>
# Posted By Dan | 2/14/08 7:21 AM
Dan - I was just going to try to analyze the results returned from searching Yahoo! Local to see the XML used to create LocalSearchItems. Thanks for the help. I created a demo of how to create your own SearchMarker objects and blogged about it here:
http://www.brucephillips.name/blog/index.cfm/2008/...
# Posted By Bruce | 2/14/08 11:40 AM
Not sure why, but the categoryFilter in the searchLocal method does not seem to work. The search results do not even contain a ycats or ycategory property? How do you filter by category?

Any help would be appreciated.
Thanks.
-CK
# Posted By Christopher Keeler | 2/19/08 1:41 PM
Zach Graves mentioned that the 0.9.0 version has a bug which is causing categoryFilter in the seachLocal method to break. There should be a new version that resolves this bug out within a week or so. Just FYI.

-Christopher Keeler
# Posted By Christopher Keeler | 2/22/08 9:54 AM
Did you try to implement the "Callout Component" from Adobe? http://www.adobe.com/devnet/flex/samples/fig_callo... It looks nice, but I can't get it to work as a custom marker... Any tips??
# Posted By Michael | 3/18/08 8:17 PM
that looks like it could be very useful. i am going to take a look at this and see if i can get it working
# Posted By Dan | 3/19/08 7:38 AM
Hey All!

i've built a marker class that you guys might be able to use. It uses a dynamic external icon, easy style change for the balloon, and click event dispatching inside of your application.

http://robotoole.com/blog/wp/?p=9


-rob
# Posted By rob | 7/7/08 8:54 AM
I also tried to use the "Callout Component" (from the Flex Interface Guide on the Adobe site, link is above) but was not successful.

My guess is that the Callout Component won't work since it's a Flex Container object that inherits from UIComponent. I didn't have any luck using any kind of UIComponent w/Yahoo's Marker class. The UIComponent is probably overriding some crucial method that prevents it from working as a normal Sprite.

I thought about trying to make the Callout Component implement the Yahoo IMarker interface, but instead I'm going to use a Yahoo LoaderMarker and load a SWF that I will create in Flash CS3 (approach suggested by Bruce above).

Not sure if that's these are the right conclusions, but loading my own SWF marker seems to be the quickest solution.
# Posted By Sunil Dave' | 7/10/08 11:28 PM
PS: Christopher Keeler has made a couple of nice looking markers you may wish to check out.

Blog post: http://www.swfflex.com/blog/1/2008/05/Yahoo-Maps-F...

The source code link is broken on that page, but it's here:
http://www.swfflex.com/flexprojects/yahoomaps/srcv...

Cheers,
Sunil
# Posted By Sunil | 7/10/08 11:33 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner