Post Pic

Google Maps API – Custom markers problem

Today I was writing a basic Flash application using the Google Maps Flash API. For those who doesn’t know, this is a very useful API for create maps (like Google Maps) inside Flash movies or Flex projects. But I had a problem when I was trying to create a custom Marker (with an image instead that default marker) and was very hard to find a solution at web or documentation, so I’ll give the solution here.


The Documentation of Google Maps Flash API is very good and a good start point. But has a little lack: no code examples for custom Marks. If you want one you’l find just Flex examples. Here’s the sample code for custom Marks in Flex:

//Image Embed
[Embed(source="myimage.png")]
private var myimage:Class;

// Create the marker
var myname = "Tooltip name";
var bm:Bitmap = new myimage() as Bitmap;
var m:Marker = new Marker(new LatLng(list[i].lat, list[i].lon),
new MarkerOptions({tooltip:myname, icon:bm}));

This works well for Flex applications but not in Flash. If you try to use this code in Flash you will get an error like this:

TypeError: Error #1007: Instantiation attempted on a non-constructor.
        at MethodInfo-1735()
        at MethodInfo-1736()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at flash.net::URLLoader/onComplete()

I made several searches in Google and tried quite a few of codes solutions and none of them works: in some cases I got new errors, in others I didn’t get errors but the marker didn’t appear. After some studying I made it with this code:

var request:URLRequest = new URLRequest("red.png");
var imageLoader:Loader = new Loader();
imageLoader.load(request);

var markerOptions:MarkerOptions = new MarkerOptions();
markerOptions.icon = imageLoader;
markerOptions.tooltip = "dom";
var m:Marker = new Marker(new LatLng(-23.6104, -46.6670), markerOptions);
map.addOverlay(m);

Got it? You just need to create a loader for your image and then use a URLRequest object to get the file. It’s so obvious now, but I spent a good time on this. I hope you don’t.

Related Posts


6 Responses

09.17.09

Great tip, Domenico-very useful! Thanks very much for posting this!

09.17.09

thanx man you save a lot of my time

09.17.09

Hello from Russia!
Can I quote a post in your blog with the link to you?

09.17.09

Thaaaanks a lot men… You are a genius! Send it to the google! lol =p

09.17.09

Thanks so much! This worked for me when lots of other sites didn’t!

09.17.09

Domenico.. I’m having hell of a time getting this to work for me..

I’m wondering if you could post the code specific to Flex 3?

I get the error that you mentioned above
TypeError: Error #1007: Instantiation attempted on a non-constructor.

I even get this when looking at the Google Maps Tutorial on their site.. http://gmaps-samples-flash.googlecode.com/svn/trunk/demos/XmlParsingDemo/XmlParsingDemo.html

I’m using Flex 3 and pulling the xml from a mysql database using php.

But the problem is with the embed tag I believe

Any Help would be most appreciated

Leave Your Response

* Name, Email, Comment are Required