How to use Google Maps API with Flash AS3
I was playing with Google Maps API for Flash when I noticed the tutorials in the official page are somehow not that smart, so I am going to clarify some things.
Follow these simple steps:
Sign up for a Google Maps API Key at this link. Once you submit the form, ignore everything you see on the page (it refers to javascript version), just write down the key.

Download the Google Maps API for Flash SDK at this link. Inside the archive you will find a lib directory with a file called map_1_8a.swc inside. The official Google docs refer to a map_1_7.swc file, so this file could be updated anytime. Just refer to the map_xxx.swf, no matter the version you are downloading.
Install the API SWC component to your Flash creating a Google folder with map_xxx.swf inside.
The path to this folder varies from CS3 to CS4, it should be placed in
\en\Configuration\Components
if you are running CS3 (watch out the en, it may differ according to your language) and
\Common\Configuration\Components
if you are running CS4.
Create a new Flash file and open the components window. You can find this window on Window -> Components menu. You should find a Google -> GoogleMapsLibrary compoent. Drag in on stage wherever you want (you will set its position later).
Don’t worry about the size.
Now it’s time to write the class.
Here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;
public class googlemap extends Sprite {
var map:Map = new Map();
public function googlemap() {
map.key = "ABQIAAAAF5GHwa7hgxz5etSP-jJVwhSK_rP-_Usta8fEpiVtC50gLzF69hQ6_VK0zgpArekpmdsRCJK2Vnp60A";
map.setSize(new Point(stage.stageWidth, stage.stageHeight));
map.addEventListener(MapEvent.MAP_READY, onMapReady);
this.addChild(map);
function onMapReady(event:Event):void {
map.setCenter(new LatLng(45.436319770227,12.33638048172), 13, MapType.NORMAL_MAP_TYPE);
}
}
}
} |
Line 4: importing the Point class in order to use a point later. This is not explained in the official tutorial as it writes the code directly into the timeline.
Lines 5-8: importing Google libraries
Line 10: declaring the new map type
Line 12: inserting the key API
Line 13: setting the size of the map to fill the entire stage, using the Point class
Line 14: listener for the map to be ready
Line 15: adding the map itself
Lines 16-18: once the map is ready, show us Venice!
Here it is:
If you give me good feedback, I’ll show you how to create custom controls and some more tricks.
They can be easily customized to meet the unique requirements of your project.
19 Responses to “How to use Google Maps API with Flash AS3”
Leave a Reply
Trackbacks
-
How to use Yahoo Maps API with Flash AS3 : Emanuele Feronato on
March 13th, 2009 11:47 pm
[...] is quite the same tutorial as How to use Google Maps API with Flash AS3, but this time we’ll learn how to use Yahoo! [...]
-
How to use Google Maps API with Flash AS3 - part 2 : Emanuele Feronato on
March 18th, 2009 12:32 pm
[...] How to use Google Maps API with Flash AS3 if you are new to Google Maps API for [...]
-
Introduction: Flash Google Maps API and Multi-touch | Cyan[c] Design on
March 22nd, 2009 3:29 am
[...] I came across Emanuele Feronato’s website, italian geek and PROgrammer. She has a tutorial to get you started using the GoogleMaps API, so here is your first task in this [...]
- weblog » Blog Archive » Googlemaps Flash API on April 26th, 2009 9:19 am
- iamnotbored.com » Blog Archive » flash google maps on May 20th, 2009 1:32 am
-
Introduction: Flash Google Maps API and Multi-touch « Kelso’s Corner on
February 11th, 2010 1:01 pm
[...] I came across Emanuele Feronato’s website, Italian geek and PROgrammer. He has a tutorial to get you started using the GoogleMaps API, so here is your first task in this [...]
- Citrus Engine released for free for learning
- My epic fail with ClickBank
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Triqui MochiAds Arcade plugin for WordPress official page
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a survival horror game in Flash tutorial – part 1 (4.74/5)
- Create a flash artillery game – step 2 (4.74/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 1 (4.71/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(24 votes, average: 4.63 out of 5)



Whoa, that’s awesome, thx for this information (too)
This is really cool.
Last time I have use the component provide by afcomponents.com, can use some of the function in the google map.
Like it, always a nice app for a site.
it works nice! is there any more fla files elsewhere, because i dont have flex…
salut!
ah! my example:
http://www.lalluvia.com/gmap/gMap.html
You forgot a couple of crucial steps:
19. Realize Google has no local search integration
20. Use Yahoo Maps instead
:)
What I am finding is that while each of the map APIs have pros and cons, Yahoo map seems to offer more in terms of functionality, unless you want to use them both in combination.
Ive also found that Google maps tends to be more sluggish than Yahoo, but ymmv.
Thanks for this, alfonso.
@TJ – You can integrate local search with Flash API via the Local Search API:
http://code.google.com/apis/maps/documentation/flash/demogallery.html?searchquery=local&classname=
do you know if you can use kml with google maps as3
Awesome, thnx!
Hey this is great, Thanks a lot for the awesome work here, I was just wondering, this works if you put the script inside the flash file, but you’re showing us the package which means its coming from an actionscript file, so my question is, how do you call it from within flash???
I know you have to go googlemap();
but it asks you for an argument inside the function caller. So my question is, what would the argument be inside the function caller?
thanks in advance for your help!
How can i use http://local.google.com/ to get reviews and ratings in flash??
I am new to the google map api for flash. I found your example helpful to understand more about how they work. I was wondering what the line
9 public class googlemap extends Sprite {
did for the map and how you would add a marker for a business for example?
Hey thanks for the good example, was really helpful. The Google tutorial was rubbish! Ciao!