forked from: ModestMapsSample
import com.adobe.viewsource.ViewSource;
package {
//import com.adobe.viewsource.ViewSource;
import com.modestmaps.Map;
import com.modestmaps.TweenMap;
import com.modestmaps.core.MapExtent;
import com.modestmaps.events.MapEvent;
import com.modestmaps.events.MarkerEvent;
import com.modestmaps.extras.MapControls;
import com.modestmaps.extras.MapCopyright;
import com.modestmaps.extras.ZoomBox;
import com.modestmaps.extras.ZoomSlider;
import com.modestmaps.geo.Location;
import com.modestmaps.mapproviders.*;
import com.modestmaps.mapproviders.microsoft.*;
import com.modestmaps.mapproviders.yahoo.*;
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
[SWF(backgroundColor="#000000", frameRate="30")]
public class Main extends Sprite
{
private var map:Map;
//private var tooltip:Tooltip;
private var status:TextField;
private var mapButtons:Sprite;
private const PADDING:int = 15;//余白サイズ
//エントリ
public function Main(){
//ViewSource.addMenuItem(this, 'srcview/index.html', true);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
createChildren();
placeMarkers();
stage.addEventListener(Event.RESIZE, onResize);
onResize();
}
//表示
private function createChildren():void{
// create map
map = new TweenMap(stage.stageWidth - 2 * PADDING, stage.stageHeight - 2 * PADDING,
true,
new MicrosoftRoadMapProvider(),//デフォルトマッププロバイダ
new MapExtent(35.70503,35.68551,-220.31546,-220.28285));//初期位置
//new MapExtent(37.829853, 37.700121, -122.212601, -122.514725));
map.addEventListener(MouseEvent.DOUBLE_CLICK, map.onDoubleClick);
map.x = map.y = PADDING;
// マップイベント
map.addEventListener(MapEvent.ZOOMED_BY, onZoomed);
map.addEventListener(MapEvent.STOP_ZOOMING, onStopZoom);
map.addEventListener(MapEvent.PANNED, onPanned);
map.addEventListener(MapEvent.STOP_PANNING, onStopPan);
map.addEventListener(MapEvent.RESIZED, onResized);
// マーカーイベント
map.addEventListener(MarkerEvent.MARKER_CLICK, onMarkerClick);
map.addEventListener(MarkerEvent.MARKER_ROLL_OVER, onMarkerRollOver);
map.addEventListener(MarkerEvent.MARKER_ROLL_OUT, onMarkerRollOut);
// 操作用インターフェース
//map.addChild(new MapControls(map));//スクロール、ズーム
map.addChild(new ZoomSlider(map));//ズーム
map.addChild(new ZoomBox(map));
var s1:Sprite = new Sprite();
s1.graphics.beginFill(0x0000ff);
s1.graphics.lineTo(50,0);
s1.graphics.lineTo(50,50);
s1.graphics.lineTo(0,50);
//s1.graphics.drawCircle(0,0,20);
s1.graphics.endFill();
var locA:Location = Location.fromString( "35.70503,-220.31546" );//座標
map.putMarker( locA, s1);
// add a default copyright handler to the map
//map.addChild(new MapCopyright(map));
// create text field to hold status text
status = new TextField();
status.defaultTextFormat = new TextFormat('Verdana', 10, 0xeeeeee);
status.selectable = false;
status.text = 'Welcome to Modest Maps...';
status.width = 600;
status.height = 20;
// マップ切り替えボタン
mapButtons = new Sprite();
mapButtons.addChild(new MapProviderButton('MS Road', map.getMapProvider(), true));
mapButtons.addChild(new MapProviderButton('MS Aerial', new MicrosoftAerialMapProvider()));
mapButtons.addChild(new MapProviderButton('MS Hybrid', new MicrosoftHybridMapProvider()));
mapButtons.addChild(new MapProviderButton('Yahoo Road', new YahooRoadMapProvider()));
//mapButtons.addChild(new MapProviderButton('Yahoo Aerial', new YahooAerialMapProvider()));
mapButtons.addChild(new MapProviderButton('Yahoo Hybrid', new YahooHybridMapProvider()));
mapButtons.addChild(new MapProviderButton('Open Street Map', new OpenStreetMapProvider()));
//mapButtons.addChild(new MapProviderButton('AC Transit', new ACTransitMapProvider()));
//mapButtons.addChild(new MapProviderButton('Daily Planet', new DailyPlanetProvider()));
// arrange buttons 22px apart
for( var n:int = 0; n < mapButtons.numChildren; n++) {
mapButtons.getChildAt(n).y = n * 22;
}
// listen for map provider button clicks
mapButtons.addEventListener(MouseEvent.CLICK, onProviderButtonClick);
mapButtons.alpha = 0.7;
// add children to the display list
addChild(map);
addChild(status);//位置情報
addChild(mapButtons);
}
private function onWheel(e:MouseEvent):void {
// e.delta;
}
//マーカー
private function placeMarkers():void {
//データ配列
var markerpoints:Array = [
{ title:'mztm.jp', loc:"35.70295, -220.29511"},
{ title:'Rochdale', loc:"37.865571, -122.259679"},
{ title:'Parker Ave.', loc:"37.780492, -122.453731"},
{ title:'Pepper Dr.', loc:"37.623443, -122.426577"},
{ title:'3rd St.', loc:"37.779297, -122.392877"},
{ title:'Divisadero St.', loc:"37.771919, -122.437413"},
{ title:'Market St.', loc:"37.812734, -122.280064"}
];
var o:Object;
//マーカー作成
for each (o in markerpoints) {
var marker:SampleMarker = new SampleMarker();//インスタンス作成
marker.title = o.title;
var loc:Location = Location.fromString( o.loc );//座標
map.putMarker( loc, marker);//マーカー登録
}
}
//
private function onResize(event:Event = null):void {
var w:Number = stage.stageWidth - 2 * PADDING;
var h:Number = stage.stageHeight - 2 * PADDING;
// position and size the map
map.x = map.y = PADDING;
map.setSize(w, h);
// align the buttons to the right
mapButtons.x = map.x + w - 120;
mapButtons.y = map.y + 10;
// place status just below the map on the left
status.width = w;
status.x = map.x + 2;
status.y = map.y + h;
}
//マップ切り替えボタン
private function onProviderButtonClick(event:Event):void {
var button:MapProviderButton = event.target as MapProviderButton;
map.setMapProvider(button.mapProvider);//セットマップ
button.selected = true;
for (var i:int = 0; i < mapButtons.numChildren; i++) {
var other:MapProviderButton = mapButtons.getChildAt(i) as MapProviderButton;
if (other != button) {
other.selected = false;
}
}
}
//マーカー クリック
private function onMarkerClick(event:MarkerEvent):void {
var marker:SampleMarker = event.marker as SampleMarker;
status.text = "Marker Clicked: " + marker.title + " " + event.location;
}
//マーカー ロールオーバー
private function onMarkerRollOver(event:MarkerEvent):void {
//trace('Roll Over ' + event.marker + event.location);
var marker:SampleMarker = event.marker as SampleMarker;
// show tooltip
var pt:Point = map.locationPoint( event.location, this );
/*
tooltip.x = pt.x;
tooltip.y = pt.y;
tooltip.label = marker.title;
tooltip.visible = true;
*/
}
//マーカー ロールアウト
private function onMarkerRollOut(event:MarkerEvent):void {
// hide the tooltip
//tooltip.visible = false;
}
//---------------------
// Map Event Handlers スクロール & ズーム
//---------------------
private function onPanned(event:MapEvent):void {
status.text = 'Panned by ' + event.panDelta.toString() + ', ';
status.appendText('top left: ' + map.getExtent().northWest.toString() + ', ');
status.appendText('bottom right: ' + map.getExtent().southEast.toString());
}
private function onStopPan(event:MapEvent):void {
status.text = 'Stopped panning, top left: ' + map.getExtent().northWest.toString() + ', ';
status.appendText('center: ' + map.getCenterZoom()[0].toString() + ', ');
status.appendText('bottom right: ' + map.getExtent().southEast.toString() + ', ');
status.appendText('zoom: ' + map.getCenterZoom()[1]);
}
private function onZoomed(event:MapEvent):void {
status.text = 'Zoomed by ' + event.zoomDelta.toFixed(3) + ', ';
status.appendText('top left: ' + map.getExtent().northWest.toString() + ', ');
status.appendText('bottom right: ' + map.getExtent().southEast.toString());
}
private function onStopZoom(event:MapEvent):void {
status.text = 'Stopped zooming, top left: ' + map.getExtent().northWest.toString() + ', ';
status.appendText('center: ' + map.getCenterZoom()[0].toString() + ', ');
status.appendText('bottom right: ' + map.getExtent().southEast.toString() + ', ');
status.appendText('zoom: ' + map.getCenterZoom()[1]);;
}
private function onResized(event:MapEvent):void
{
status.text = 'Resized to: ' + event.newSize[0] + ' x ' + event.newSize[1];
}
}
}
//マッププロバイダ選択ボタン
//MapProviderButton.as
import com.modestmaps.mapproviders.IMapProvider;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.geom.ColorTransform;
import flash.text.TextField;
import flash.text.TextFormat;
class MapProviderButton extends Sprite {
private var label:TextField;
public var mapProvider:IMapProvider;
private var overTransform:ColorTransform = new ColorTransform(1,1,1);
private var outTransform:ColorTransform = new ColorTransform(1,.9,.6);
private var normalFormat:TextFormat = new TextFormat( 'Verdana', 10, 0x000000, false);
private var selectedFormat:TextFormat = new TextFormat( 'Verdana', 10, 0x000000, true);
private var _selected:Boolean = false;
public function MapProviderButton( label_text:String, map_provider:IMapProvider, selected:Boolean=false ) {
useHandCursor = true;
mouseChildren = false;
buttonMode = true;
addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
mapProvider = map_provider;
filters = [ new DropShadowFilter(1,45,0,1,3,3,.7,2) ];
transform.colorTransform = outTransform;
// create label
label = new TextField();
label.selectable = false;
label.defaultTextFormat = normalFormat;
label.text = label_text;
label.width = label.textWidth + 8;
label.height = 18;
label.x = label.y=1;
addChild( label );
this.selected = selected;
// create background
graphics.clear();
graphics.beginFill(0xdddddd);
graphics.drawRoundRect(0, 0, 110, 18, 9, 9);
graphics.beginFill(0xffffff);
graphics.drawRoundRect(0, 0, 108, 16, 9, 9);
graphics.beginFill(0xbbbbbb);
graphics.drawRoundRect(2, 2, 108, 16, 9, 9);
graphics.beginFill(0xdddddd);
graphics.drawRoundRect(1, 1, 108, 16, 9, 9);
}
public function onMouseOver(event:MouseEvent=null):void {
transform.colorTransform = overTransform;
}
public function onMouseOut(event:MouseEvent=null):void{
transform.colorTransform = outTransform;
}
public function set selected(s:Boolean):void{
_selected = s;
label.setTextFormat(s ? selectedFormat : normalFormat);
}
public function get selected():Boolean{
return _selected;
}
}
//SampleMarker.as
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.BevelFilter;
class SampleMarker extends Sprite {
private var _title : String;
public function SampleMarker() {
buttonMode = true;
mouseChildren = false;
tabEnabled = false;
//cacheAsBitmap = true;
mouseEnabled = true;
graphics.beginFill(0x33ff00);
//graphics.drawCircle(0, 0, 10);
graphics.drawRect(-10,-10,20,20);
graphics.endFill();
filters = [ new BevelFilter(1,45,0xffffff,0.35,0x000000,0.35,2,2,1,1) ];
addEventListener( MouseEvent.MOUSE_OVER, mouseOver );
}
public function get title () : String {
return _title;
}
public function set title (s:String) : void {
_title = s;
}
protected function mouseOver(e:MouseEvent) : void {
parent.swapChildrenAt(parent.getChildIndex(this), parent.numChildren - 1);
}
override public function toString() : String {
return '[SampleMarker] ' + title;
}
}