How to Use a Google API With AS3

Google offers a map API -- an application programming interface -- for programs that use the ActionScript 3 programming language. The API makes it easy to add Google maps of anywhere in the world to AS3-based applications like games, learning tools and business software. You can incorporate a map API into an AS3 program using the Adobe Flash Professional platform, in just a few minutes. Developing advanced map applications requires familiarity with Google AS3 map methods and properties.

Things You'll Need

  • Google API map key
  • Google API map software development kit
  • Adobe Flash Professional: CS3, CS4 or CS5
Show More

Instructions

    • 1

      Obtain the Google API map key from the Google website. Save it to paste into the AS3 API map program later.

    • 2

      Download the Google map interface zip file (the map sdk directory) from the Google website.

    • 3

      Open the interface zip file, and navigate to the "lib" directory within the "sdk" directory. Select the "map_1_20" file in the directory, and move it to the "components" directory within your Adobe Flash program directory, if you are using Adobe Flash Professional software to create the AS3 map program.

    • 4

      Start the Flash program. Click "Flash File(ActionScript 3.0)" from the splash window, to create a new AS3 file for the map program.

    • 5

      Click "component" from the "Window" menu. Click and drag the "GoogleMapsLibrary" component to the Flash stage.

    • 6

      Select "Actions" from the "Window" menu on the main Flash menu bar, to open the AS3 editor. Position your mouse cursor on the first line of the AS3 editor. Click your mouse button, and type in the code below to import the necessary Google API utilities needed to access and run the Google map program.

      import com.google.maps.LatLng;

      import com.google.maps.Map;

      import com.google.maps.MapEvent;

      import com.google.maps.MapType;

    • 7

      Type the code starting at the next line of the AS3 editor, to create a copy (instance) of the Google map component.

      var myMap:Map = new Map();

    • 8

      Type the code starting at the next line of the AS3 editor to assign the Google API map key obtained, the "key" property of the map instance "myMap."

      myMap.key = "ABQIAAAAIpc3YswUCIpXrroYCBRVH3t8bmDSUSlTZ6iLt2x2i0mr5xQea9CYBu_E0AiXLFMrijp-A";

      The map key will look similar to the string enclosed in quotes above. The characters in the string, however, will be unique to the map key generated for your account.

    • 9

      Type the code starting at the next line of the AS3 editor, to set the "sensor" property of the myMap instance. Set the sensor property to "false" for this example.

      myMap.sensor = "false";

      The "sensor" property would be set to true, if the device you are using has a sensor that detects its longitude and latitude (such as a GPS phone) and your program requires you to display a map based on those coordinates.

    • 10

      Type the code starting at the next line of the AS3 editor, to resize the map so that it matches the width and height dimensions of the window you are displaying the map in.

      myMap.setSize(new Point(stage.stageWidth, stage.stageHeight));

    • 11

      Type the code starting at the next line of the AS3 editor, to detect when the map is ready for display and place the specific map (as specified with the longitude and latitude arguments in the setCenter method).

      myMap.addEventListener(MapEvent.MAP_READY, displayMap);

      this.addChild(myMap);

      function displayMap(event:Event):void {

      myMap.setCenter(new LatLng(38.45,-122.7), 14, MapType.NORMAL_MAP_TYPE);

      }

      In the code above, the level of zoom is set to 14. Increase or decrease this number to zoom in or zoom out of a region in the map.

Learnify Hub © www.0685.com All Rights Reserved