sven
sven

Reputation: 23

Actionscript 3 with Google Maps API: How to make an interactive overlay

I have a Google Map in my Flash project with a polygon overlay.

Is it possible to add to that overlay an EventListener so it triggers a function?

All I can find in the API reference is something called "MapMouseEvent" but I can't figure out if that is what I need or how it works.

Upvotes: 2

Views: 782

Answers (1)

Jason Sturges
Jason Sturges

Reputation: 15955

You can use MapMouseEvents from the Google Maps API.

Here an example using the polyline code from Google, and then adding an event listener for mouse clicks:


package
{
    import com.google.maps.LatLng;
    import com.google.maps.Map;
    import com.google.maps.MapEvent;
    import com.google.maps.MapMouseEvent;
    import com.google.maps.MapType;
    import com.google.maps.overlays.Polyline;
    import com.google.maps.overlays.PolylineOptions;
    import com.google.maps.styles.StrokeStyle;

    import flash.display.Sprite;
    import flash.geom.Point;

    public class Test extends Sprite
    {

        private var map:Map;

        public function Test()
        {
            map = new Map();
            map.key = "YOUR_API_KEY";
            map.sensor = "false";
            map.setSize(new Point(stage.stageWidth, stage.stageHeight));
            addChild(map);

            map.addEventListener(MapEvent.MAP_READY, mapReadyHandler);
        }

        public function mapReadyHandler(event:MapEvent):void
        {
            map.setCenter(new LatLng(37.4419, -122.1419), 13, MapType.NORMAL_MAP_TYPE);

            var polyline:Polyline = new Polyline([
                                                 new LatLng(37.4419, -122.1419),
                                                 new LatLng(37.4519, -122.1519)
                                                 ], new PolylineOptions({strokeStyle: new StrokeStyle({
                                                                                                          color: 0xFF0000,
                                                                                                          thickness: 4,
                                                                                                          alpha: 0.7})
                                                                        }));
            polyline.addEventListener(MapMouseEvent.CLICK, polylineClickHandler);
            map.addOverlay(polyline);
        }

        protected function polylineClickHandler(event:MapMouseEvent):void
        {
            trace("polyline clicked.");
        }

    }
}

Upvotes: 1

Related Questions