習作2009/02/13
2月 13, 2009 · Posted in Flash · コメントは受け付けていません。
Action Script
package {
import flash.display.MovieClip;
import flash.display.Stage;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;
import com.google.maps.controls.OverviewMapControl;
import com.google.maps.MapEvent;
import com.google.maps.MapMouseEvent;
import com.google.maps.Map;
import com.google.maps.MapType;
import com.google.maps.LatLng;
import com.google.maps.LatLngBounds;
import com.google.maps.ProjectionBase;
import com.google.maps.interfaces.IProjection;
import com.google.maps.interfaces.IMapType;
import com.google.maps.controls.MapTypeControl;
import com.google.maps.controls.ZoomControl;
import com.google.maps.controls.PositionControl;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
import com.google.maps.styles.StrokeStyle;
import com.google.maps.styles.FillStyle;
public class Gmap extends MovieClip {
const APP_ID:String = "api key";
var mapContainer:MovieClip;
public var googleMap:Map;
//コンストラクタ
public function Gmap():void{
init();
}
//Google MAP 作成
function init():void {
//ムービークリップ作成
mapContainer = new MovieClip();
mapContainer.x = mapContainer.y = 0;
//Google MAP 作成
googleMap = new Map();
googleMap.key = APP_ID;
googleMap.language = "ja";
googleMap.setSize(new Point(stage.stageWidth, stage.stageHeight)); //サイズ設定
googleMap.addControl(new PositionControl());
googleMap.addControl(new ZoomControl());
googleMap.addControl(new MapTypeControl());
googleMap.addControl(new OverviewMapControl());
googleMap.addEventListener(MapEvent.MAP_READY, onMapReady, false, 0, true); //イベント設定
//配置
addChild(mapContainer);
mapContainer.addChild(googleMap);
}
//地図生成完了イベント
function onMapReady(e:MapEvent):void {
googleMap.removeEventListener(MapEvent.MAP_READY, onMapReady);
//初期表示設定
googleMap.enableScrollWheelZoom();
googleMap.enableContinuousZoom();
googleMap.setCenter(new LatLng(34.985458, 135.757755), 16, MapType.NORMAL_MAP_TYPE);
//マーカー追加
addMarkers();
//ステージ設定
initStage();
}
//マーカー配置
function addMarkers():void {
//マーカー1設定
var marker1:Marker = new Marker(new LatLng(34.985458, 135.757755));
marker1.addEventListener(MapMouseEvent.CLICK, clicked, false, 1, true);
//マーカー配置
googleMap.addOverlay(marker1);
}
//マーカークリックイベント
function clicked(event:MapMouseEvent):void {
googleMap.panTo(event.target.getLatLng());
}
//ステージ設定
public function initStage():void {
stage.showDefaultContextMenu = false;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, stageResizeListener);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener, true);
}
//マウス移動イベント
function mouseMoveListener(e:MouseEvent):void {
//マウスの座標取得
var nowLL:LatLng = googleMap.fromViewportToLatLng(new Point(mouseX, mouseY));
trace(nowLL.lat()+" , "+nowLL.lng());
}
//ステージリサイズイベント
public function stageResizeListener(e:Event):void {
googleMap.setSize(new Point(stage.stageWidth, stage.stageHeight));
}
}
}

