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.


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 :)
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
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>
http://www.brucephillips.name/blog/index.cfm/2008/...
Any help would be appreciated.
Thanks.
-CK
-Christopher Keeler
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
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.
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