Changeset 297
- Timestamp:
- 06/15/07 19:21:23 (1 year ago)
- Files:
-
- trunk/as3/lib/com/modestmaps/core/TileGrid.as (modified) (26 diffs)
- trunk/as3/lib/com/modestmaps/Map.as (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/as3/lib/com/modestmaps/core/TileGrid.as
r292 r297 6 6 package com.modestmaps.core 7 7 { 8 import com.modestmaps.Map;9 import com.modestmaps.mapproviders.IMapProvider;10 import com.modestmaps.core.*;11 import com.modestmaps.geo.Location;12 import com.stamen.twisted.*;8 import com.modestmaps.Map; 9 import com.modestmaps.mapproviders.IMapProvider; 10 import com.modestmaps.core.*; 11 import com.modestmaps.geo.Location; 12 import com.stamen.twisted.*; 13 13 14 import flash.geom.Point;15 import flash.display.Sprite;16 import flash.utils.Dictionary;17 import flash.geom.Rectangle;18 import flash.events.MouseEvent;19 import flash.geom.Transform;20 import flash.geom.Matrix;21 import flash.events.Event;22 import flash.display.Stage;23 24 public class TileGrid extends Sprite25 {26 // Real maps use 256.27 public static const TILE_WIDTH:Number = 256;28 public static const TILE_HEIGHT:Number = 256;29 30 protected var _map:Map;31 32 protected var _width:Number;33 protected var _height:Number;34 protected var _draggable:Boolean;35 36 // Row and column counts are kept up-to-date.37 protected var _rows:int;38 protected var _columns:int;39 protected var _tiles:/*Tile*/Array;40 41 // overlay markers42 protected var markers:MarkerSet;43 44 // Markers overlapping the currently-included set of tiles, hash of booleans45 protected var _overlappingMarkers:Dictionary;46 47 // Allow (true) or prevent (false) tiles to paint themselves.48 protected var _paintingAllowed:Boolean;49 50 // Starting point for the very first tile51 protected var _initTilePoint:Point;52 protected var _initTileCoord:Coordinate;53 54 // the currently-native zoom level55 public var zoomLevel:int;56 57 // some limits on scrolling distance, initially set to none58 protected var topLeftOutLimit:Coordinate;59 protected var bottomRightInLimit:Coordinate;60 61 protected var _startingWellPosition:Point;62 63 // Tiles attach to the well.64 protected var _well:Sprite;65 66 // Mask clip to hide outside edges of tiles.67 protected var _mask:Sprite;68 69 // Active when the well is being dragged on the stage.70 protected var _wellDragTask:DelayedCall;71 72 // Defines a ring of extra, masked-out tiles around73 // the edges of the well, acting as a pre-fetching cache.74 // High tileBuffer may hurt performance.75 protected var _tileBuffer:int = 0;76 77 // Who do we get our Map graphics from?78 protected var _mapProvider:IMapProvider;79 80 protected var _drawWell:Boolean = true;81 protected var _drawGridArea:Boolean = true;82 83 public function init(width:Number, height:Number, draggable:Boolean, provider:IMapProvider, map:Map):void84 {85 if (!Reactor.running())86 throw new Error('com.modestmaps.core.TileGrid.init(): com.stamen.Twisted.Reactor really ought to be running at this point. Seriously.');87 88 _map = map;89 _width = width;90 _height = height;91 _draggable = draggable;92 _mapProvider = provider;93 94 buildWell();95 buildMask();96 allowPainting(true);97 redraw();98 99 _overlappingMarkers = new Dictionary(true);100 markers = new MarkerSet(this);101 102 setInitialTile(new Coordinate(0,0,1), new Point(-TILE_WIDTH, -TILE_HEIGHT));103 initializeTiles();104 }105 106 /**107 * Set initTileCoord and initTilePoint for use by initializeTiles().14 import flash.geom.Point; 15 import flash.display.Sprite; 16 import flash.utils.Dictionary; 17 import flash.geom.Rectangle; 18 import flash.events.MouseEvent; 19 import flash.geom.Transform; 20 import flash.geom.Matrix; 21 import flash.events.Event; 22 import flash.display.Stage; 23 24 public class TileGrid extends Sprite 25 { 26 // Real maps use 256. 27 public static const TILE_WIDTH:Number = 256; 28 public static const TILE_HEIGHT:Number = 256; 29 30 protected var _map:Map; 31 32 protected var _width:Number; 33 protected var _height:Number; 34 protected var _draggable:Boolean; 35 36 // Row and column counts are kept up-to-date. 37 protected var _rows:int; 38 protected var _columns:int; 39 protected var _tiles:/*Tile*/Array; 40 41 // overlay markers 42 protected var markers:MarkerSet; 43 44 // Markers overlapping the currently-included set of tiles, hash of booleans 45 protected var _overlappingMarkers:Dictionary; 46 47 // Allow (true) or prevent (false) tiles to paint themselves. 48 protected var _paintingAllowed:Boolean; 49 50 // Starting point for the very first tile 51 protected var _initTilePoint:Point; 52 protected var _initTileCoord:Coordinate; 53 54 // the currently-native zoom level 55 public var zoomLevel:int; 56 57 // some limits on scrolling distance, initially set to none 58 protected var topLeftOutLimit:Coordinate; 59 protected var bottomRightInLimit:Coordinate; 60 61 protected var _startingWellPosition:Point; 62 63 // Tiles attach to the well. 64 protected var _well:Sprite; 65 66 // Mask clip to hide outside edges of tiles. 67 protected var _mask:Sprite; 68 69 // Active when the well is being dragged on the stage. 70 protected var _wellDragTask:DelayedCall; 71 72 // Defines a ring of extra, masked-out tiles around 73 // the edges of the well, acting as a pre-fetching cache. 74 // High tileBuffer may hurt performance. 75 protected var _tileBuffer:int = 0; 76 77 // Who do we get our Map graphics from? 78 protected var _mapProvider:IMapProvider; 79 80 protected var _wellColor:uint = 0x000000; 81 protected var _gridAreaColor:uint = 0x000000; 82 83 public function init(width:Number, height:Number, draggable:Boolean, provider:IMapProvider, map:Map):void 84 { 85 if (!Reactor.running()) 86 throw new Error('com.modestmaps.core.TileGrid.init(): com.stamen.Twisted.Reactor really ought to be running at this point. Seriously.'); 87 88 _map = map; 89 _width = width; 90 _height = height; 91 _draggable = draggable; 92 _mapProvider = provider; 93 94 buildWell(); 95 buildMask(); 96 allowPainting(true); 97 redraw(); 98 99 _overlappingMarkers = new Dictionary(true); 100 markers = new MarkerSet(this); 101 102 setInitialTile(new Coordinate(0,0,1), new Point(-TILE_WIDTH, -TILE_HEIGHT)); 103 initializeTiles(); 104 } 105 106 /** 107 * Set initTileCoord and initTilePoint for use by initializeTiles(). 108 108 */ 109 109 public function setInitialTile(coord:Coordinate, point:Point):void … … 119 119 public function resetTiles(coord:Coordinate, point:Point):void 120 120 { 121 //trace('resetting tiles...');121 //trace('resetting tiles...'); 122 122 if (!_tiles) 123 {124 //trace("no _tiles for resetTiles() yet");123 { 124 //trace("no _tiles for resetTiles() yet"); 125 125 setInitialTile(coord, point); 126 126 return; 127 127 } 128 128 129 //trace('REALLY resetting tiles...');129 //trace('REALLY resetting tiles...'); 130 130 131 try {132 var initTile:Tile;133 var condemnedTiles:/*Tile*/Array = activeTiles();131 try { 132 var initTile:Tile; 133 var condemnedTiles:/*Tile*/Array = activeTiles(); 134 134 135 for (var i:int = 0; i < condemnedTiles.length; i++)136 {137 condemnedTiles[i].expire();138 }135 for (var i:int = 0; i < condemnedTiles.length; i++) 136 { 137 condemnedTiles[i].expire(); 138 } 139 139 140 Reactor.callLater(condemnationDelay(), destroyTiles, condemnedTiles);140 Reactor.callLater(condemnationDelay(), destroyTiles, condemnedTiles); 141 141 142 zoomLevel = coord.zoom;143 initTile = createTile(this, coord, point.x, point.y);142 zoomLevel = coord.zoom; 143 initTile = createTile(this, coord, point.x, point.y); 144 144 145 centerWell(true);145 centerWell(true); 146 146 147 _rows = 1;148 _columns = 1;147 _rows = 1; 148 _columns = 1; 149 149 150 allocateTiles();151 }152 catch(e:Error) {153 trace(e.getStackTrace());154 }150 allocateTiles(); 151 } 152 catch(e:Error) { 153 trace(e.getStackTrace()); 154 } 155 155 156 156 } … … 165 165 // trace('initializing...'); 166 166 167 if (!_initTileCoord) {168 trace("no _initTileCoord");169 return;170 }167 if (!_initTileCoord) { 168 trace("no _initTileCoord"); 169 return; 170 } 171 171 172 172 // impose some limits … … 175 175 bottomRightInLimit = _mapProvider.outerLimits()[1]; 176 176 177 //trace('REALLY initializing, like _tiles and shit...');178 179 _tiles = [];177 //trace('REALLY initializing, like _tiles and shit...'); 178 179 _tiles = []; 180 180 initTile = createTile(this, _initTileCoord, _initTilePoint.x, _initTilePoint.y); 181 181 … … 221 221 _well.name = 'well'; 222 222 223 if (_draggable) { 224 _well.mouseChildren = false; 225 _well.addEventListener(MouseEvent.MOUSE_DOWN, startWellDrag); 226 _well.addEventListener(MouseEvent.MOUSE_UP, stopWellDrag); 227 } 223 if (_draggable) 224 { 225 _well.mouseChildren = false; 226 _well.addEventListener(MouseEvent.MOUSE_DOWN, startWellDrag); 227 _well.addEventListener(MouseEvent.MOUSE_UP, stopWellDrag); 228 } 228 229 229 230 addChild(_well); … … 259 260 260 261 if (_mapProvider.geometry() != previousGeometry) 261 {262 { 262 263 markers.initializeIndex(); 263 264 markers.indexAtZoom(zoomLevel); … … 287 288 protected function destroyTile(tile:Tile):void 288 289 { 289 //trace('Destroying tile: '+tile.toString());290 //trace('Destroying tile: '+tile.toString()); 290 291 _tiles.splice(tileIndex(tile), 1); 291 292 tile.cancelDraw(); … … 299 300 { 300 301 if (tiles.length) 301 {302 { 302 303 destroyTile(Tile(tiles.shift())); 303 304 Reactor.callLater(0, destroyTiles, tiles); … … 448 449 max.y = _well.y - max.y; 449 450 450 ///trace('min/max for drag: '+min+', '+max+' ('+topLeftOutLimit+', '+bottomRightInLimit+')');451 //trace('min/max for drag: '+min+', '+max+' ('+topLeftOutLimit+', '+bottomRightInLimit+')'); 451 452 452 453 // weird negative edge conditions, limit all movement on an axis … … 466 467 public function startWellDrag(event:MouseEvent):void 467 468 { 468 stage.addEventListener(MouseEvent.MOUSE_UP, stopWellDrag);469 stage.addEventListener(MouseEvent.MOUSE_UP, stopWellDrag); 469 470 stage.addEventListener(MouseEvent.MOUSE_OUT, stopWellDrag); 470 471 … … 498 499 : bounds.max.y); 499 500 500 //trace('Drag bounds would be: '+xMin+', '+yMin+', '+xMax+', '+yMax);501 502 _startingWellPosition = new Point(_well.x, _well.y);503 //trace('Starting well position: '+_startingWellPosition.toString());501 //trace('Drag bounds would be: '+xMin+', '+yMin+', '+xMax+', '+yMax); 502 503 _startingWellPosition = new Point(_well.x, _well.y); 504 //trace('Starting well position: '+_startingWellPosition.toString()); 504 505 505 506 _map.onStartPan(); … … 519 520 _map.onStopPan(); 520 521 if (_wellDragTask) { 521 _wellDragTask.call(); // issue final onPan, notify markers, etc.522 _wellDragTask.call(); // issue final onPan, notify markers, etc. 522 523 _wellDragTask.cancel(); // but cancel the follow-on call 523 524 } … … 550 551 normalizeWell(); 551 552 allocateTiles(); 552 //trace('New well scale: '+_well.scaleX.toString());553 //trace('New well scale: '+_well.scaleX.toString()); 553 554 } 554 555 } … … 627 628 var matches:Array = new Array(); 628 629 if (_tiles) { 629 matches = _tiles.filter(function(item:Tile, index:int, list:Array):Boolean { return item.isActive();} );630 if (matches.length == 0) {631 trace("no matches for active tiles... DOOM!");632 }633 }630 matches = _tiles.filter(function(item:Tile, index:int, list:Array):Boolean { return item.isActive();} ); 631 if (matches.length == 0) { 632 trace("no matches for active tiles... DOOM!"); 633 } 634 } 634 635 return matches; 635 636 } … … 742 743 centerWell(true); 743 744 744 trace("well scale: " + _well.scaleX + " " + _well.scaleY);745 trace("well scale: " + _well.scaleX + " " + _well.scaleY); 745 746 if(Math.abs(_well.scaleX - 1.0) < 0.01) { 746 747 active = activeTiles(); … … 773 774 //trace('This is where we scale the whole well by '+zoomAdjust+' zoom levels: '+(100 / scaleAdjust)+'%'); 774 775 775 var n:int;776 var n:int; 776 777 for (n = 0; n < zoomAdjust; n += 1) 777 {778 { 778 779 splitTiles(); 779 780 zoomLevel += 1; … … 781 782 782 783 for (n = 0; n > zoomAdjust; n -= 1) 783 {784 { 784 785 mergeTiles(); 785 786 zoomLevel -= 1; … … 838 839 // this should never happen 839 840 if(!referenceTile) { 840 trace("TileGrid problem - no reference tile");841 trace("TileGrid problem - no reference tile"); 841 842 return; 842 843 } … … 844 845 // this should never happen either 845 846 if(!referenceTile.coord) { 846 trace("TileGrid problem - no coord in reference tile");847 trace("TileGrid problem - no coord in reference tile"); 847 848 return; 848 849 } … … 906 907 // this should never happen 907 908 if(!referenceTile) { 908 throw new Error("no reference tile in mergeTiles()");909 throw new Error("no reference tile in mergeTiles()"); 909 910 } 910 911 911 912 // this should never happen either 912 913 if(!referenceTile.coord) { 913 throw new Error("no reference tile coord in mergeTiles()");914 throw new Error("no reference tile coord in mergeTiles()"); 914 915 } 915 916 … … 1009 1010 newOverlappingMarkers[visible[i].id] = visible[i]; 1010 1011 1011 var id:String;1012 var id:String; 1012 1013 // check for newly-visible markers 1013 1014 for (id in newOverlappingMarkers) { … … 1102 1103 return function(a:Tile, b:Tile):Number 1103 1104 { 1104 // TODO: can probably nix the sqrt if we're just sorting by distance1105 // FYI: this whole method isn't really ever used, it can probably just go away entirely1105 // TODO: can probably nix the sqrt if we're just sorting by distance 1106 // FYI: this whole method isn't really ever used, it can probably just go away entirely 1106 1107 var aDist:Number = Math.sqrt(Math.pow(a.center().x - p.x, 2) + Math.pow(a.center().y - p.y, 2)); 1107 1108 var bDist:Number = Math.sqrt(Math.pow(b.center().x - p.x, 2) + Math.pow(b.center().y - p.y, 2)); … … 1117 1118 if(a.coord.row == b.coord.row) { 1118 1119 return a.coord.column - b.coord.column; 1119 1120 1120 } else { 1121 1121 return a.coord.row - b.coord.row; 1122 1123 1122 } 1124 1123 } … … 1131 1130 if(a.coord.column == b.coord.column) { 1132 1131 return a.coord.row - b.coord.row; 1133 1134 1132 } else { 1135 1133 return a.coord.column - b.coord.column; 1136 1137 1134 } 1138 1135 } … … 1163 1160 } 1164 1161 1165 // set to false, and set drawGridArea to false, if you want the background swf color to show through 1166 public function set drawWell(draw:Boolean):void {1167 _drawWell = draw;1168 redrawWell();1169 }1170 // set to false, and set drawWell to false, if you want the background swf color to show through 1171 public function set drawGridArea(draw:Boolean):void {1172 _drawGridArea = draw;1173 redrawGridArea();1174 }1162 /** you can probably set to a color with no alpha if you want the grid area color to show through */ 1163 public function set wellColor(color:uint):void { 1164 _wellColor = color; 1165 redrawWell(); 1166 } 1167 /** you can probably set to a color with no alpha if you want the background swf color to show through */ 1168 public function set drawGridArea(color:uint):void { 1169 _gridAreaColor = color; 1170 redrawGridArea(); 1171 } 1175 1172 1176 1173 protected function redraw():void { … … 1184 1181 with (graphics) 1185 1182 { 1186 clear(); 1187 if (_drawGridArea) { 1188 moveTo(0, 0); 1189 // lineStyle(2, 0x990099, 100); 1190 beginFill(0x666666, 0.2); 1191 lineTo(0, _height); 1192 lineTo(_width, _height); 1193 lineTo(_width, 0); 1194 lineTo(0, 0); 1195 endFill(); 1196 } 1197 } 1198 } 1199 1183 clear(); 1184 moveTo(0, 0); 1185 // lineStyle(2, 0x990099, 100); 1186 beginFill(_gridAreaColor); 1187 lineTo(0, _height); 1188 lineTo(_width, _height); 1189 lineTo(_width, 0); 1190 lineTo(0, 0); 1191 endFill(); 1192 } 1193 } 1194 1200 1195 protected function redrawMask():void 1201 {1196 { 1202 1197 with (_mask.graphics) 1203 1198 { 1204 clear(); 1205 moveTo(0, 0); 1206 // lineStyle(2, 0x990099, 100); 1207 lineStyle(); 1208 beginFill(0x000000, 0); 1209 lineTo(0, _height); 1210 lineTo(_width, _height); 1211 lineTo(_width, 0); 1212 lineTo(0, 0); 1213 endFill(); 1214 } 1199 clear(); 1200 moveTo(0, 0); 1201 lineStyle(); 1202 beginFill(0x000000, 0); 1203 lineTo(0, _height); 1204 lineTo(_width, _height); 1205 lineTo(_width, 0); 1206 lineTo(0, 0); 1207 endFill(); 1208 } 1215 1209 } 1216 1210 1217 1211 protected function redrawWell():void 1218 {1212 { 1219 1213 // note that _well (0, 0) is grid center. 1220 1214 with (_well.graphics) 1221 1215 { 1222 clear(); 1223 if (_drawWell) { 1224 moveTo(_width/-2, _height/-2); 1225 lineStyle(); 1226 beginFill(0x666666, 0.2); 1227 lineTo(_width/-2, _height/2); 1228 lineTo(_width/2, _height/2); 1229 lineTo(_width/2, _height/-2); 1230 lineTo(_width/-2, _height/-2); 1231 endFill(); 1232 } 1216 clear(); 1217 moveTo(_width/-2, _height/-2); 1218 lineStyle(); 1219 beginFill(_wellColor); 1220 lineTo(_width/-2, _height/2); 1221 lineTo(_width/2, _height/2); 1222 lineTo(_width/2, _height/-2); 1223 lineTo(_width/-2, _height/-2); 1224 endFill(); 1233 1225 } 1234 1226 } trunk/as3/lib/com/modestmaps/Map.as
r292 r297 10 10 * 11 11 * @description Map is the base class and interface for Modest Maps. 12 * Correctly attaching an instance of this Sprite subclass13 * should result in a pannable map. Controls and event14 * handlers must be added separately.12 * Correctly attaching an instance of this Sprite subclass 13 * should result in a pannable map. Controls and event 14 * handlers must be added separately. 15 15 * 16 16 * @usage <code> … … 27 27 package com.modestmaps 28 28 { 29 import com.modestmaps.core.*;30 import com.modestmaps.events.MapEvent;31 import com.modestmaps.events.MarkerEvent;32 import com.modestmaps.geo.Location;33 import com.modestmaps.mapproviders.IMapProvider;34 import com.stamen.twisted.DelayedCall;35 import com.stamen.twisted.Reactor;36 37 import flash.display.Sprite;38 import flash.geom.Point;39 import flash.events.MouseEvent;40 import flash.external.ExternalInterface;41 import flash.events.Event;42 43 public class Map extends Sprite44 {45 public static const PAN:String = 'pan';46 public static const ZOOM:String = 'zoom';47 48 protected var __width:Number = 320;49 protected var __height:Number = 240;50 protected var __draggable:Boolean = true;51 52 // pending animation steps, array of {type:'pan'/'zoom', amount:Point/Number, redraw:Boolean}53 protected var __animSteps:Array;54 55 // associated animation call56 protected var __animTask:DelayedCall;57 58 // frames-per-2x-zoom59 public var zoomFrames:Number = 6;60 61 // frames-per-full-pan62 public var panFrames:Number = 12;63 64 protected var __startingPosition:Point;65 protected var __currentPosition:Point;66 protected var __startingZoom:Number;67 protected var __currentZoom:Number;68 69 // das grid70 public var grid:TileGrid;71 72 // Who do we get our Map graphics from?73 protected var __mapProvider:IMapProvider;74 75 /** htmlText to be added to a label - listen for MapEvent.COPYRIGHT_CHANGED */76 public var copyright:String = "";77 78 /*79 * Initialize the map: set properties, add a tile grid, draw it.80 * This method must be called before the map can be used!81 * Default extent covers the entire globe, (+/-85, +/-180).82 *83 * @param Width of map, in pixels.84 * @param Height of map, in pixels.85 * @param Whether the map can be dragged or not.86 * @param Desired map provider, e.g. Blue Marble.87 *88 * @see com.modestmaps.core.TileGrid89 */90 public function init(width:Number, height:Number, draggable:Boolean, provider:IMapProvider):void91 {92 if (!Reactor.running())93 {94 // should this really be fatal?95 trace('com.modestmaps.Map.init(): com.stamen.Twisted.Reactor ought to be running at this point.');96 Reactor.run(this, 100);97 }98 99 try { 100 ExternalInterface.addCallback("setCopyright", setCopyright); 101 } 102 catch (error:Error) { 103 trace("problem adding setCopyright as callback in Map.as"); 104 trace(error.getStackTrace());105 } 106 107 108 __animSteps = new Array();109 110 setSize(width, height);111 112 grid = new TileGrid();113 addChild(grid); // before init, so init can add mouse handlers to stage114 grid.init(__width, __height, draggable, provider, this);115 116 setMapProvider(provider);117 118 var extent:MapExtent = new MapExtent(85, -85, 180, -180);119 setExtent(extent);120 121 }122 123 /*124 * Based on an array of locations, determine appropriate map125 * bounds using calculateMapExtent(), and inform the grid of126 * tile coordinate and point by calling grid.resetTiles().127 * Resulting map extent will ensure that all passed locations128 * are visible.129 *130 * @param Array of locations.131 *132 * @see com.modestmaps.Map#calculateMapExtent133 * @see com.modestmaps.core.TileGrid#resetTiles134 */135 public function setExtent(extent:MapExtent):void136 {137 var position:MapPosition = extentPosition(extent);138 // tell grid what the rock is cooking139 grid.resetTiles(position.coord, position.point);140 onExtentChanged(this.getExtent());29 import com.modestmaps.core.*; 30 import com.modestmaps.events.MapEvent; 31 import com.modestmaps.events.MarkerEvent; 32 import com.modestmaps.geo.Location; 33 import com.modestmaps.mapproviders.IMapProvider; 34 import com.stamen.twisted.DelayedCall; 35 import com.stamen.twisted.Reactor; 36 37 import flash.display.Sprite; 38 import flash.geom.Point; 39 import flash.events.MouseEvent; 40 import flash.external.ExternalInterface; 41 import flash.events.Event; 42 43 public class Map extends Sprite 44 { 45 public static const PAN:String = 'pan'; 46 public static const ZOOM:String = 'zoom'; 47 48 protected var __width:Number = 320; 49 protected var __height:Number = 240; 50 protected var __draggable:Boolean = true; 51 52 // pending animation steps, array of {type:'pan'/'zoom', amount:Point/Number, redraw:Boolean} 53 protected var __animSteps:Array; 54 55 // associated animation call 56 protected var __animTask:DelayedCall; 57 58 // frames-per-2x-zoom 59 public var zoomFrames:Number = 6; 60 61 // frames-per-full-pan 62 public var panFrames:Number = 12; 63 64 protected var __startingPosition:Point; 65 protected var __currentPosition:Point; 66 protected var __startingZoom:Number; 67 protected var __currentZoom:Number; 68 69 // das grid 70 public var grid:TileGrid; 71 72 // Who do we get our Map graphics from? 73 protected var __mapProvider:IMapProvider; 74 75 /** htmlText to be added to a label - listen for MapEvent.COPYRIGHT_CHANGED */ 76 public var copyright:String = ""; 77 78 /* 79 * Initialize the map: set properties, add a tile grid, draw it. 80 * This method must be called before the map can be used! 81 * Default extent covers the entire globe, (+/-85, +/-180). 82 * 83 * @param Width of map, in pixels. 84 * @param Height of map, in pixels. 85 * @param Whether the map can be dragged or not. 86 * @param Desired map provider, e.g. Blue Marble. 87 * 88 * @see com.modestmaps.core.TileGrid 89 */ 90 public function init(width:Number, height:Number, draggable:Boolean, provider:IMapProvider):void 91 { 92 if (!Reactor.running()) 93 { 94 // should this really be fatal? 95 trace('com.modestmaps.Map.init(): com.stamen.Twisted.Reactor ought to be running at this point.'); 96 Reactor.run(this, 100); 97 } 98 99 // see setCopyright for more details 100 try { 101 ExternalInterface.addCallback("setCopyright", setCopyright); 102 } 103 catch (error:Error) { 104 trace("problem adding setCopyright as callback in Map.as"); 105 trace(error.getStackTrace()); 106 } 107 108 __animSteps = new Array(); 109 110 setSize(width, height); 111 112 grid = new TileGrid(); 113 addChild(grid); // before init, so init can add mouse handlers to stage 114 grid.init(__width, __height, draggable, provider, this); 115 116 setMapProvider(provider); 117 118 var extent:MapExtent = new MapExtent(85, -85, 180, -180); 119 setExtent(extent); 120 121 } 122 123 /* 124 * Based on an array of locations, determine appropriate map 125 * bounds using calculateMapExtent(), and inform the grid of 126 * tile coordinate and point by calling grid.resetTiles(). 127 * Resulting map extent will ensure that all passed locations 128 * are visible. 129 * 130 * @param Array of locations. 131 * 132 * @see com.modestmaps.Map#calculateMapExtent 133 * @see com.modestmaps.core.TileGrid#resetTiles 134 */ 135 public function setExtent(extent:MapExtent):void 136 { 137 var position:MapPosition = extentPosition(extent); 138 // tell grid what the rock is cooking 139 grid.resetTiles(position.coord, position.point); 140 onExtentChanged(this.getExtent()); 141 141 Reactor.callNextFrame(callCopyright); 142 }143 144 /*145 * Based on a location and zoom level, determine appropriate initial146 * tile coordinate and point using calculateMapCenter(), and inform147 * the grid of tile coordinate and point by calling grid.resetTiles().148 *149 * @param Location of center.150 * @param Desired zoom level.151 *152 * @see com.modestmaps.Map#calculateMapExtent153 * @see com.modestmaps.core.TileGrid#resetTiles154 */155 public function setCenterZoom(location:Location, zoom:Number):void156 {157 var center:MapPosition = coordinatePosition(__mapProvider.locationCoordinate(location).zoomTo(zoom));158 // tell grid what the rock is cooking159
