- 2010年5月10日 04:13
- flash
Google Maps API for Flash の Map クラスは、 座標を指定するメソッドが setCenter のみなので、地図の好きな場所に中心座標(設定した緯度経度)を移動できるようにしてみました。
package {
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;
import com.google.maps.LatLng;
import com.google.maps.MapOptions;
import com.google.maps.LatLngBounds;
import com.google.maps.overlays.Marker;
import com.google.maps.overlays.MarkerOptions;
// マーカー用の画像
import modules.CenterMark;
public class Index extends Sprite {
// 緯度経度
private static var LATLNG:LatLng = new LatLng( 34.687416, 135.525767 );
// 倍率
private static const ZOOM:int = 16;
private var map:Map;
private var center:Point;
private var current:Point;
public function Index() {
addEventListener(Event.ADDED_TO_STAGE, function(evt:Event):void {
evt.target.removeEventListener(Event.ADDED_TO_STAGE, arguments.callee);
init();
});
}
private function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// マップを生成
map = new Map();
map.setSize( new Point( stage.stageWidth, stage.stageHeight ) );
map.language = "en";
map.key = "ABQIAAAAENIXH1hznwJBA793YortxhS4tSKS1TvmptmkUjwmK2MYNf0RkRTFY5UNYQsMEAF3NrvWbS7oTanFIA";
map.addEventListener(MapEvent.MAP_PREINITIALIZE, mapPreinitializeHandler);
map.addEventListener(MapEvent.MAP_READY, mapReadyHandler);
map.mouseEnabled = false;
map.mouseChildren = false;
// ロード
addChild( map );
}
private function mapPreinitializeHandler(evt:MapEvent):void {
// 航空写真だけをロード
map.setInitOptions(
new MapOptions({
mapTypes: [MapType.SATELLITE_MAP_TYPE]
})
);
}
private function mapReadyHandler(evt:MapEvent):void {
// 初期設定
map.setCenter(LATLNG, ZOOM, MapType.SATELLITE_MAP_TYPE);
// マーカーを設置
var option:MarkerOptions = new MarkerOptions({
icon: new CenterMark(),
hasShadow: false,
clickable: false,
draggable: false
});
var marker:Marker = new Marker(LATLNG, option);
map.addOverlay( marker );
// 初期中心座標を求める
center = map.fromLatLngToPoint(LATLNG, ZOOM);
current = new Point(100, 100);
// 移動
moveCenter( current, false );
stage.addEventListener(Event.RESIZE, resizeHandler);
stage.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(evt:MouseEvent):void {
current = new Point( mouseX, mouseY );
moveCenter( current );
}
private function moveCenter( p:Point, move:Boolean=true ):void {
// クリックした座標から中心座標を計算
var target:Point = new Point( center.x + Math.round(map.width/2) - p.x, center.y + Math.round(map.height/2) - p.y );
// 緯度経度に変換
var latlng:LatLng = map.fromPointToLatLng( target, ZOOM)
// 移動
if( move ) map.panTo( latlng );
else map.setCenter(latlng, ZOOM, MapType.SATELLITE_MAP_TYPE);
}
private function resizeHandler(evt:Event=null):void {
// マップサイズを変更
map.setSize( new Point( stage.stageWidth, stage.stageHeight ) );
center = map.fromLatLngToPoint(LATLNG, ZOOM);
moveCenter( current, false );
}
}
}
- Newer: Happy New Year
- Older: 新年会