| 41 | | [Event(name="startZooming", type="com.modestmaps.events.MapEvent")] |
|---|
| 42 | | [Event(name="stopZooming", type="com.modestmaps.events.MapEvent")] |
|---|
| 43 | | [Event(name="zoomedBy", type="com.modestmaps.events.MapEvent")] |
|---|
| 44 | | [Event(name="startPanning", type="com.modestmaps.events.MapEvent")] |
|---|
| 45 | | [Event(name="stopPanning", type="com.modestmaps.events.MapEvent")] |
|---|
| 46 | | [Event(name="pannedBy", type="com.modestmaps.events.MapEvent")] |
|---|
| 47 | | [Event(name="resized", type="com.modestmaps.events.MapEvent")] |
|---|
| 48 | | [Event(name="copyrightChanged", type="com.modestmaps.events.MapEvent")] |
|---|
| 49 | | [Event(name="extentChanged", type="com.modestmaps.events.MapEvent")] |
|---|
| 50 | | [Event(name="markerEnter", type="com.modestmaps.events.MarkerEvent")] |
|---|
| 51 | | [Event(name="markerLeave", type="com.modestmaps.events.MarkerEvent")] |
|---|
| 52 | | import flash.geom.Rectangle; |
|---|
| 53 | | |
|---|
| 54 | | import com.modestmaps.mapproviders.microsoft.MicrosoftHybridMapProvider; |
|---|
| 55 | | public class Map extends Sprite |
|---|
| | 41 | [Event(name="startZooming", type="com.modestmaps.events.MapEvent")] |
|---|
| | 42 | [Event(name="stopZooming", type="com.modestmaps.events.MapEvent")] |
|---|
| | 43 | [Event(name="zoomedBy", type="com.modestmaps.events.MapEvent")] |
|---|
| | 44 | [Event(name="startPanning", type="com.modestmaps.events.MapEvent")] |
|---|
| | 45 | [Event(name="stopPanning", type="com.modestmaps.events.MapEvent")] |
|---|
| | 46 | [Event(name="pannedBy", type="com.modestmaps.events.MapEvent")] |
|---|
| | 47 | [Event(name="resized", type="com.modestmaps.events.MapEvent")] |
|---|
| | 48 | [Event(name="copyrightChanged", type="com.modestmaps.events.MapEvent")] |
|---|
| | 49 | [Event(name="extentChanged", type="com.modestmaps.events.MapEvent")] |
|---|
| | 50 | [Event(name="markerEnter", type="com.modestmaps.events.MarkerEvent")] |
|---|
| | 51 | [Event(name="markerLeave", type="com.modestmaps.events.MarkerEvent")] |
|---|
| | 52 | import flash.geom.Rectangle; |
|---|
| | 53 | |
|---|
| | 54 | import com.modestmaps.mapproviders.microsoft.MicrosoftHybridMapProvider; |
|---|
| | 55 | public class Map extends Sprite |
|---|
| 60 | | protected var __width:Number = 320; |
|---|
| 61 | | protected var __height:Number = 240; |
|---|
| 62 | | protected var __draggable:Boolean = true; |
|---|
| 63 | | |
|---|
| 64 | | // pending animation steps, array of {type:'pan'/'zoom', amount:Point/Number, redraw:Boolean} |
|---|
| 65 | | protected var __animSteps:Array = []; |
|---|
| 66 | | |
|---|
| 67 | | // associated animation call |
|---|
| 68 | | protected var __animTask:DelayedCall; |
|---|
| 69 | | |
|---|
| 70 | | // frames-per-2x-zoom |
|---|
| 71 | | public var zoomFrames:Number = 6; |
|---|
| 72 | | |
|---|
| 73 | | // frames-per-full-pan |
|---|
| 74 | | public var panFrames:Number = 12; |
|---|
| 75 | | |
|---|
| 76 | | protected var __startingPosition:Point; |
|---|
| 77 | | protected var __currentPosition:Point; |
|---|
| 78 | | protected var __startingZoom:Number; |
|---|
| 79 | | protected var __currentZoom:Number; |
|---|
| 80 | | |
|---|
| 81 | | // das grid |
|---|
| 82 | | public var grid:TileGrid; |
|---|
| | 60 | protected var __width:Number = 320; |
|---|
| | 61 | protected var __height:Number = 240; |
|---|
| | 62 | protected var __draggable:Boolean = true; |
|---|
| | 63 | |
|---|
| | 64 | // pending animation steps, array of {type:'pan'/'zoom', amount:Point/Number, redraw:Boolean} |
|---|
| | 65 | protected var __animSteps:Array = []; |
|---|
| | 66 | |
|---|
| | 67 | // associated animation call |
|---|
| | 68 | protected var __animTask:DelayedCall; |
|---|
| | 69 | |
|---|
| | 70 | // frames-per-2x-zoom |
|---|
| | 71 | public var zoomFrames:Number = 6; |
|---|
| | 72 | |
|---|
| | 73 | // frames-per-full-pan |
|---|
| | 74 | public var panFrames:Number = 12; |
|---|
| | 75 | |
|---|
| | 76 | protected var __startingPosition:Point; |
|---|
| | 77 | protected var __currentPosition:Point; |
|---|
| | 78 | protected var __startingZoom:Number; |
|---|
| | 79 | protected var __currentZoom:Number; |
|---|
| | 80 | |
|---|
| | 81 | // das grid |
|---|
| | 82 | public var grid:TileGrid; |
|---|
| 93 | | /* |
|---|
| 94 | | * Initialize the map: set properties, add a tile grid, draw it. |
|---|
| 95 | | * Default extent covers the entire globe, (+/-85, +/-180). |
|---|
| 96 | | * |
|---|
| 97 | | * @param Width of map, in pixels. |
|---|
| 98 | | * @param Height of map, in pixels. |
|---|
| 99 | | * @param Whether the map can be dragged or not. |
|---|
| 100 | | * @param Desired map provider, e.g. Blue Marble. |
|---|
| 101 | | * |
|---|
| 102 | | * @see com.modestmaps.core.TileGrid |
|---|
| 103 | | */ |
|---|
| 104 | | public function Map(width:Number=320, height:Number=240, draggable:Boolean=true, provider:IMapProvider=null, extent:MapExtent=null) |
|---|
| 105 | | { |
|---|
| 106 | | if(provider==null) provider = new MicrosoftHybridMapProvider(); |
|---|
| 107 | | if(extent==null) extent = new MapExtent(85, -85, 180, -180); |
|---|
| 108 | | |
|---|
| 109 | | if (!Reactor.running()) |
|---|
| 110 | | { |
|---|
| 111 | | // should this really be fatal? |
|---|
| 112 | | //trace('com.modestmaps.Map.init(): com.stamen.Twisted.Reactor ought to be running at this point.'); |
|---|
| 113 | | Reactor.run(this, 100); |
|---|
| 114 | | } |
|---|
| | 93 | /* |
|---|
| | 94 | * Initialize the map: set properties, add a tile grid, draw it. |
|---|
| | 95 | * Default extent covers the entire globe, (+/-85, +/-180). |
|---|
| | 96 | * |
|---|
| | 97 | * @param Width of map, in pixels. |
|---|
| | 98 | * @param Height of map, in pixels. |
|---|
| | 99 | * @param Whether the map can be dragged or not. |
|---|
| | 100 | * @param Desired map provider, e.g. Blue Marble. |
|---|
| | 101 | * |
|---|
| | 102 | * @see com.modestmaps.core.TileGrid |
|---|
| | 103 | */ |
|---|
| | 104 | public function Map(width:Number=320, height:Number=240, draggable:Boolean=true, provider:IMapProvider=null, extent:MapExtent=null) |
|---|
| | 105 | { |
|---|
| | 106 | if(provider==null) provider = new MicrosoftHybridMapProvider(); |
|---|
| | 107 | if(extent==null) extent = new MapExtent(85, -85, 180, -180); |
|---|
| | 108 | |
|---|
| | 109 | if (!Reactor.running()) |
|---|
| | 110 | { |
|---|
| | 111 | // should this really be fatal? |
|---|
| | 112 | //trace('com.modestmaps.Map.init(): com.stamen.Twisted.Reactor ought to be running at this point.'); |
|---|
| | 113 | Reactor.run(this, 100); |
|---|
| | 114 | } |
|---|
| 135 | | setMapProvider(provider); |
|---|
| 136 | | setExtent(extent); |
|---|
| 137 | | } |
|---|
| 138 | | |
|---|
| 139 | | |
|---|
| 140 | | |
|---|
| 141 | | /* |
|---|
| 142 | | * Based on an array of locations, determine appropriate map |
|---|
| 143 | | * bounds using calculateMapExtent(), and inform the grid of |
|---|
| 144 | | * tile coordinate and point by calling grid.resetTiles(). |
|---|
| 145 | | * Resulting map extent will ensure that all passed locations |
|---|
| 146 | | * are visible. |
|---|
| 147 | | * |
|---|
| 148 | | * @param Array of locations. |
|---|
| 149 | | * |
|---|
| 150 | | * @see com.modestmaps.Map#calculateMapExtent |
|---|
| 151 | | * @see com.modestmaps.core.TileGrid#resetTiles |
|---|
| 152 | | */ |
|---|
| 153 | | public function setExtent(extent:MapExtent):void |
|---|
| 154 | | { |
|---|
| 155 | | var position:MapPosition = extentPosition(extent); |
|---|
| 156 | | // tell grid what the rock is cooking |
|---|
| 157 | | grid.resetTiles(position.coord, position.point); |
|---|
| 158 | | onExtentChanged(this.getExtent()); |
|---|
| 159 | | Reactor.callNextFrame(callCopyright); |
|---|
| 160 | | } |
|---|
| 161 | | |
|---|
| | 135 | setMapProvider(provider); |
|---|
| | 136 | setExtent(extent); |
|---|
| | 137 | } |
|---|
| | 138 | |
|---|
| | 139 | |
|---|
| | 140 | |
|---|
| | 141 | /* |
|---|
| | 142 | * Based on an array of locations, determine appropriate map |
|---|
| | 143 | * bounds using calculateMapExtent(), and inform the grid of |
|---|
| | 144 | * tile coordinate and point by calling grid.resetTiles(). |
|---|
| | 145 | * Resulting map extent will ensure that all passed locations |
|---|
| | 146 | * are visible. |
|---|
| | 147 | * |
|---|
| | 148 | * @param Array of locations. |
|---|
| | 149 | * |
|---|
| | 150 | * @see com.modestmaps.Map#calculateMapExtent |
|---|
| | 151 | * @see com.modestmaps.core.TileGrid#resetTiles |
|---|
| | 152 | */ |
|---|
| | 153 | public function setExtent(extent:MapExtent):void |
|---|
| | 154 | { |
|---|
| | 155 | var position:MapPosition = extentPosition(extent); |
|---|
| | 156 | // tell grid what the rock is cooking |
|---|
| | 157 | grid.resetTiles(position.coord, position.point); |
|---|
| | 158 | onExtentChanged(this.getExtent()); |
|---|
| | 159 | Reactor.callNextFrame(callCopyright); |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| 163 | | * Based on a location and zoom level, determine appropriate initial |
|---|
| 164 | | * tile coordinate and point using calculateMapCenter(), and inform |
|---|
| 165 | | * the grid of tile coordinate and point by calling grid.resetTiles(). |
|---|
| 166 | | * |
|---|
| 167 | | * @param Location of center. |
|---|
| 168 | | * @param Desired zoom level. |
|---|
| 169 | | * |
|---|
| 170 | | * @see com.modestmaps.Map#calculateMapExtent |
|---|
| 171 | | * @see com.modestmaps.core.TileGrid#resetTiles |
|---|
| 172 | | */ |
|---|
| 173 | | public function setCenterZoom(location:Location, zoom:Number):void |
|---|
| 174 | | { |
|---|
| 175 | | if (zoom == grid.zoomLevel) { |
|---|
| 176 | | setCenter(location); |
|---|
| 177 | | } |
|---|
| 178 | | else { |
|---|
| 179 | | var center:MapPosition = coordinatePosition(__mapProvider.locationCoordinate(location).zoomTo(zoom)); |
|---|
| 180 | | // tell grid what the rock is cooking |
|---|
| 181 | | grid.resetTiles(center.coord, center.point); |
|---|
| 182 | | onExtentChanged(this.getExtent()); |
|---|
| 183 | | Reactor.callNextFrame(callCopyright); |
|---|
| 184 | | } |
|---|
| 185 | | } |
|---|
| | 163 | * Based on a location and zoom level, determine appropriate initial |
|---|
| | 164 | * tile coordinate and point using calculateMapCenter(), and inform |
|---|
| | 165 | * the grid of tile coordinate and point by calling grid.resetTiles(). |
|---|
| | 166 | * |
|---|
| | 167 | * @param Location of center. |
|---|
| | 168 | * @param Desired zoom level. |
|---|
| | 169 | * |
|---|
| | 170 | * @see com.modestmaps.Map#calculateMapExtent |
|---|
| | 171 | * @see com.modestmaps.core.TileGrid#resetTiles |
|---|
| | 172 | */ |
|---|
| | 173 | public function setCenterZoom(location:Location, zoom:Number):void |
|---|
| | 174 | { |
|---|
| | 175 | if (zoom == grid.zoomLevel) { |
|---|
| | 176 | setCenter(location); |
|---|
| | 177 | } |
|---|
| | 178 | else { |
|---|
| | 179 | var center:MapPosition = coordinatePosition(__mapProvider.locationCoordinate(location).zoomTo(zoom)); |
|---|
| | 180 | // tell grid what the rock is cooking |
|---|
| | 181 | grid.resetTiles(center.coord, center.point); |
|---|
| | 182 | onExtentChanged(this.getExtent()); |
|---|
| | 183 | Reactor.callNextFrame(callCopyright); |
|---|
| | 184 | } |
|---|
| | 185 | } |
|---|
| 187 | | /* |
|---|
| 188 | | * Based on a zoom level, determine appropriate initial |
|---|
| 189 | | * tile coordinate and point using calculateMapCenter(), and inform |
|---|
| 190 | | * the grid of tile coordinate and point by calling grid.resetTiles(). |
|---|
| 191 | | * |
|---|
| 192 | | * @param Desired zoom level. |
|---|
| 193 | | * |
|---|
| 194 | | * @see com.modestmaps.Map#calculateMapExtent |
|---|
| 195 | | * @see com.modestmaps.core.TileGrid#resetTiles |
|---|
| 196 | | */ |
|---|
| 197 | | public function setZoom(zoom:Number):void |
|---|
| 198 | | { |
|---|
| 199 | | if (zoom == grid.zoomLevel) { // do nothing! |
|---|
| 200 | | return; |
|---|
| 201 | | } |
|---|
| 202 | | else if (zoom - grid.zoomLevel == 1) { // if 1 step in, delegate to zoomIn animation |
|---|
| 203 | | zoomIn(); |
|---|
| 204 | | } |
|---|
| 205 | | else if (zoom - grid.zoomLevel == -1) { // if 1 step out, delegate to zoomOut animation |
|---|
| 206 | | zoomOut(); |
|---|
| 207 | | } |
|---|
| 208 | | else { // else hard reset |
|---|
| 209 | | var center:MapPosition = coordinatePosition(grid.centerCoordinate().zoomTo(zoom)); |
|---|
| 210 | | // tell grid what the rock is cooking |
|---|
| 211 | | grid.resetTiles(center.coord, center.point); |
|---|
| 212 | | onExtentChanged(this.getExtent()); |
|---|
| 213 | | Reactor.callNextFrame(callCopyright); |
|---|
| 214 | | } |
|---|
| 215 | | } |
|---|
| 216 | | |
|---|
| | 187 | /* |
|---|
| | 188 | * Based on a zoom level, determine appropriate initial |
|---|
| | 189 | * tile coordinate and point using calculateMapCenter(), and inform |
|---|
| | 190 | * the grid of tile coordinate and point by calling grid.resetTiles(). |
|---|
| | 191 | * |
|---|
| | 192 | * @param Desired zoom level. |
|---|
| | 193 | * |
|---|
| | 194 | * @see com.modestmaps.Map#calculateMapExtent |
|---|
| | 195 | * @see com.modestmaps.core.TileGrid#resetTiles |
|---|
| | 196 | */ |
|---|
| | 197 | public function setZoom(zoom:Number):void |
|---|
| | 198 | { |
|---|
| | 199 | if (zoom == grid.zoomLevel) { // do nothing! |
|---|
| | 200 | return; |
|---|
| | 201 | } |
|---|
| | 202 | else if (zoom - grid.zoomLevel == 1) { // if 1 step in, delegate to zoomIn animation |
|---|
| | 203 | zoomIn(); |
|---|
| | 204 | } |
|---|
| | 205 | else if (zoom - grid.zoomLevel == -1) { // if 1 step out, delegate to zoomOut animation |
|---|
| | 206 | zoomOut(); |
|---|
| | 207 | } |
|---|
| | 208 | else { // else hard reset |
|---|
| | 209 | var center:MapPosition = coordinatePosition(grid.centerCoordinate().zoomTo(zoom)); |
|---|
| | 210 | // tell grid what the rock is cooking |
|---|
| | 211 | grid.resetTiles(center.coord, center.point); |
|---|
| | 212 | onExtentChanged(this.getExtent()); |
|---|
| | 213 | Reactor.callNextFrame(callCopyright); |
|---|
| | 214 | } |
|---|
| | 215 | } |
|---|
| | 216 | |
|---|
| 218 | | * Based on a coordinate, determine appropriate starting tile and position, |
|---|
| 219 | | * and return a two-element object with a coord and a point. |
|---|
| 220 | | */ |
|---|
| 221 | | protected function coordinatePosition(centerCoord:Coordinate):MapPosition |
|---|
| 222 | | { |
|---|
| 223 | | // initial tile coordinate |
|---|
| 224 | | var initTileCoord:Coordinate = new Coordinate( Math.floor(centerCoord.row), |
|---|
| 225 | | Math.floor(centerCoord.column), |
|---|
| 226 | | Math.floor(centerCoord.zoom)); |
|---|
| 227 | | |
|---|
| 228 | | // initial tile position, assuming centered tile well in grid |
|---|
| 229 | | var initX:Number = (initTileCoord.column - centerCoord.column) * TileGrid.TILE_WIDTH; |
|---|
| 230 | | var initY:Number = (initTileCoord.row - centerCoord.row) * TileGrid.TILE_HEIGHT; |
|---|
| 231 | | var initPoint:Point = new Point(Math.round(initX), Math.round(initY)); |
|---|
| 232 | | |
|---|
| 233 | | return new MapPosition(initTileCoord, initPoint); |
|---|
| 234 | | } |
|---|
| | 218 | * Based on a coordinate, determine appropriate starting tile and position, |
|---|
| | 219 | * and return a two-element object with a coord and a point. |
|---|
| | 220 | */ |
|---|
| | 221 | protected function coordinatePosition(centerCoord:Coordinate):MapPosition |
|---|
| | 222 | { |
|---|
| | 223 | // initial tile coordinate |
|---|
| | 224 | var initTileCoord:Coordinate = new Coordinate( Math.floor(centerCoord.row), |
|---|
| | 225 | Math.floor(centerCoord.column), |
|---|
| | 226 | Math.floor(centerCoord.zoom)); |
|---|
| | 227 | |
|---|
| | 228 | // initial tile position, assuming centered tile well in grid |
|---|
| | 229 | var initX:Number = (initTileCoord.column - centerCoord.column) * TileGrid.TILE_WIDTH; |
|---|
| | 230 | var initY:Number = (initTileCoord.row - centerCoord.row) * TileGrid.TILE_HEIGHT; |
|---|
| | 231 | var initPoint:Point = new Point(Math.round(initX), Math.round(initY)); |
|---|
| | 232 | |
|---|
| | 233 | return new MapPosition(initTileCoord, initPoint); |
|---|
| | 234 | } |
|---|
| 239 | | // my kingdom for a decent map() function in AS2 |
|---|
| 240 | | var coordinates:Array = new Array(); |
|---|
| 241 | | |
|---|
| 242 | | for(var i:Number = 0; i < locations.length; i += 1) |
|---|
| 243 | | coordinates.unshift(__mapProvider.locationCoordinate(locations[i])); |
|---|
| 244 | | |
|---|
| 245 | | // get outermost top left and bottom right coordinates to cover all locations |
|---|
| 246 | | var TL:Coordinate = new Coordinate(coordinates[0].row, coordinates[0].column, coordinates[0].zoom); |
|---|
| 247 | | var BR:Coordinate = new Coordinate(coordinates[0].row, coordinates[0].column, coordinates[0].zoom); |
|---|
| 248 | | |
|---|
| 249 | | while (coordinates.length) |
|---|
| 250 | | { |
|---|
| 251 | | TL = new Coordinate(Math.min(TL.row, coordinates[0].row), |
|---|
| | 239 | // my kingdom for a decent map() function in AS2 |
|---|
| | 240 | var coordinates:Array = new Array(); |
|---|
| | 241 | |
|---|
| | 242 | for(var i:Number = 0; i < locations.length; i += 1) |
|---|
| | 243 | coordinates.unshift(__mapProvider.locationCoordinate(locations[i])); |
|---|
| | 244 | |
|---|
| | 245 | // get outermost top left and bottom right coordinates to cover all locations |
|---|
| | 246 | var TL:Coordinate = new Coordinate(coordinates[0].row, coordinates[0].column, coordinates[0].zoom); |
|---|
| | 247 | var BR:Coordinate = new Coordinate(coordinates[0].row, coordinates[0].column, coordinates[0].zoom); |
|---|
| | 248 | |
|---|
| | 249 | while (coordinates.length) |
|---|
| | 250 | { |
|---|
| | 251 | TL = new Coordinate(Math.min(TL.row, coordinates[0].row), |
|---|
| 257 | | coordinates.shift(); |
|---|
| 258 | | } |
|---|
| 259 | | |
|---|
| 260 | | // multiplication factor between horizontal span and map width |
|---|
| 261 | | var hFactor:Number = (BR.column - TL.column) / (__width / TileGrid.TILE_WIDTH); |
|---|
| 262 | | |
|---|
| 263 | | // multiplication factor expressed as base-2 logarithm, for zoom difference |
|---|
| 264 | | var hZoomDiff:Number = Math.log(hFactor) / Math.log(2); |
|---|
| 265 | | |
|---|
| 266 | | // possible horizontal zoom to fit geographical extent in map width |
|---|
| 267 | | var hPossibleZoom:Number = TL.zoom - Math.ceil(hZoomDiff); |
|---|
| 268 | | |
|---|
| 269 | | // multiplication factor between vertical span and map height |
|---|
| 270 | | var vFactor:Number = (BR.row - TL.row) / (__height / TileGrid.TILE_HEIGHT); |
|---|
| 271 | | |
|---|
| 272 | | // multiplication factor expressed as base-2 logarithm, for zoom difference |
|---|
| 273 | | var vZoomDiff:Number = Math.log(vFactor) / Math.log(2); |
|---|
| 274 | | |
|---|
| 275 | | // possible vertical zoom to fit geographical extent in map height |
|---|
| 276 | | var vPossibleZoom:Number = TL.zoom - Math.ceil(vZoomDiff); |
|---|
| 277 | | |
|---|
| 278 | | // initial zoom to fit extent vertically and horizontally |
|---|
| 279 | | // additionally, make sure it's not outside the boundaries set by provider limits |
|---|
| 280 | | var initZoom:Number = Math.min(hPossibleZoom, vPossibleZoom); |
|---|
| 281 | | initZoom = Math.min(initZoom, __mapProvider.outerLimits()[1].zoom); |
|---|
| 282 | | initZoom = Math.max(initZoom, __mapProvider.outerLimits()[0].zoom); |
|---|
| 283 | | |
|---|
| 284 | | // coordinate of extent center |
|---|
| 285 | | var centerRow:Number = (TL.row + BR.row) / 2; |
|---|
| 286 | | var centerColumn:Number = (TL.column + BR.column) / 2; |
|---|
| 287 | | var centerZoom:Number = (TL.zoom + BR.zoom) / 2; |
|---|
| 288 | | var centerCoord:Coordinate = (new Coordinate(centerRow, centerColumn, centerZoom)).zoomTo(initZoom); |
|---|
| 289 | | |
|---|
| 290 | | return coordinatePosition(centerCoord); |
|---|
| | 257 | coordinates.shift(); |
|---|
| | 258 | } |
|---|
| | 259 | |
|---|
| | 260 | // multiplication factor between horizontal span and map width |
|---|
| | 261 | var hFactor:Number = (BR.column - TL.column) / (__width / TileGrid.TILE_WIDTH); |
|---|
| | 262 | |
|---|
| | 263 | // multiplication factor expressed as base-2 logarithm, for zoom difference |
|---|
| | 264 | var hZoomDiff:Number = Math.log(hFactor) / Math.log(2); |
|---|
| | 265 | |
|---|
| | 266 | // possible horizontal zoom to fit geographical extent in map width |
|---|
| | 267 | var hPossibleZoom:Number = TL.zoom - Math.ceil(hZoomDiff); |
|---|
| | 268 | |
|---|
| | 269 | // multiplication factor between vertical span and map height |
|---|
| | 270 | var vFactor:Number = (BR.row - TL.row) / (__height / TileGrid.TILE_HEIGHT); |
|---|
| | 271 | |
|---|
| | 272 | // multiplication factor expressed as base-2 logarithm, for zoom difference |
|---|
| | 273 | var vZoomDiff:Number = Math.log(vFactor) / Math.log(2); |
|---|
| | 274 | |
|---|
| | 275 | // possible vertical zoom to fit geographical extent in map height |
|---|
| | 276 | var vPossibleZoom:Number = TL.zoom - Math.ceil(vZoomDiff); |
|---|
| | 277 | |
|---|
| | 278 | // initial zoom to fit extent vertically and horizontally |
|---|
| | 279 | // additionally, make sure it's not outside the boundaries set by provider limits |
|---|
| | 280 | var initZoom:Number = Math.min(hPossibleZoom, vPossibleZoom); |
|---|
| | 281 | initZoom = Math.min(initZoom, __mapProvider.outerLimits()[1].zoom); |
|---|
| | 282 | initZoom = Math.max(initZoom, __mapProvider.outerLimits()[0].zoom); |
|---|
| | 283 | |
|---|
| | 284 | // coordinate of extent center |
|---|
| | 285 | var centerRow:Number = (TL.row + BR.row) / 2; |
|---|
| | 286 | var centerColumn:Number = (TL.column + BR.column) / 2; |
|---|
| | 287 | var centerZoom:Number = (TL.zoom + BR.zoom) / 2; |
|---|
| | 288 | var centerCoord:Coordinate = (new Coordinate(centerRow, centerColumn, centerZoom)).zoomTo(initZoom); |
|---|
| | 289 | |
|---|
| | 290 | return coordinatePosition(centerCoord); |
|---|
| 294 | | * Based on an array of locations, determine appropriate map bounds |
|---|
| 295 | | * in terms of tile grid, and return a two-element object with a coord |
|---|
| 296 | | * and a point from calculateMapCenter(). |
|---|
| 297 | | */ |
|---|
| 298 | | protected function extentPosition(extent:MapExtent):MapPosition |
|---|
| 299 | | { |
|---|
| 300 | | // trace("calculateMapCenterFromExtent" + extent); |
|---|
| 301 | | // trace(new Error().getStackTrace()); |
|---|
| 302 | | var locations:Array = new Array(extent.northWest, extent.southEast); |
|---|
| 303 | | return locationsPosition(locations); |
|---|
| 304 | | } |
|---|
| | 294 | * Based on an array of locations, determine appropriate map bounds |
|---|
| | 295 | * in terms of tile grid, and return a two-element object with a coord |
|---|
| | 296 | * and a point from calculateMapCenter(). |
|---|
| | 297 | */ |
|---|
| | 298 | protected function extentPosition(extent:MapExtent):MapPosition |
|---|
| | 299 | { |
|---|
| | 300 | // trace("calculateMapCenterFromExtent" + extent); |
|---|
| | 301 | // trace(new Error().getStackTrace()); |
|---|
| | 302 | var locations:Array = new Array(extent.northWest, extent.southEast); |
|---|
| | 303 | return locationsPosition(locations); |
|---|
| | 304 | } |
|---|
| 307 | | * Return a MapExtent for the current map view. |
|---|
| 308 | | * TODO: MapExtent needs adapting to deal with non-rectangular map projections |
|---|
| 309 | | * |
|---|
| 310 | | * @return MapExtent object |
|---|
| 311 | | */ |
|---|
| 312 | | public function getExtent():MapExtent |
|---|
| 313 | | { |
|---|
| 314 | | var extent:MapExtent = new MapExtent(); |
|---|
| 315 | | |
|---|
| 316 | | if(!__mapProvider) |
|---|
| 317 | | return extent; |
|---|
| 318 | | |
|---|
| 319 | | var TL:Coordinate = grid.topLeftCoordinate(); |
|---|
| 320 | | var BR:Coordinate = grid.bottomRightCoordinate(); |
|---|
| 321 | | |
|---|
| 322 | | extent.northWest = __mapProvider.coordinateLocation(TL); |
|---|
| 323 | | extent.southEast = __mapProvider.coordinateLocation(BR); |
|---|
| 324 | | return extent; |
|---|
| 325 | | } |
|---|
| | 307 | * Return a MapExtent for the current map view. |
|---|
| | 308 | * TODO: MapExtent needs adapting to deal with non-rectangular map projections |
|---|
| | 309 | * |
|---|
| | 310 | * @return MapExtent object |
|---|
| | 311 | */ |
|---|
| | 312 | public function getExtent():MapExtent |
|---|
| | 313 | { |
|---|
| | 314 | var extent:MapExtent = new MapExtent(); |
|---|
| | 315 | |
|---|
| | 316 | if(!__mapProvider) |
|---|
| | 317 | return extent; |
|---|
| | 318 | |
|---|
| | 319 | var TL:Coordinate = grid.topLeftCoordinate(); |
|---|
| | 320 | var BR:Coordinate = grid.bottomRightCoordinate(); |
|---|
| | 321 | |
|---|
| | 322 | extent.northWest = __mapProvider.coordinateLocation(TL); |
|---|
| | 323 | extent.southEast = __mapProvider.coordinateLocation(BR); |
|---|
| | 324 | return extent; |
|---|
| | 325 | } |
|---|
| 348 | | * Return the current zoom level of the map. |
|---|
| 349 | | * |
|---|
| 350 | | * @return zoom number |
|---|
| 351 | | */ |
|---|
| 352 | | public function getZoom():int |
|---|
| 353 | | { |
|---|
| 354 | | return grid.zoomLevel; |
|---|
| 355 | | } |
|---|
| 356 | | |
|---|
| 357 | | |
|---|
| 358 | | /** |
|---|
| 359 | | * Set new map size, call onResized(). |
|---|
| 360 | | * |
|---|
| 361 | | * @param New map width. |
|---|
| 362 | | * @param New map height. |
|---|
| 363 | | * |
|---|
| 364 | | * @see com.modestmaps.Map#onResized |
|---|
| 365 | | */ |
|---|
| 366 | | public function setSize(width:Number, height:Number):void |
|---|
| 367 | | { |
|---|
| 368 | | __width = width; |
|---|
| 369 | | __height = height; |
|---|
| 370 | | if (grid) |
|---|
| 371 | | { |
|---|
| 372 | | grid.resizeTo(new Point(__width, __height)); |
|---|
| 373 | | } |
|---|
| 374 | | onResized(); |
|---|
| 375 | | |
|---|
| 376 | | // mask out out of bounds marker remnants |
|---|
| 377 | | scrollRect = new Rectangle(0,0,width,height); |
|---|
| 378 | | } |
|---|
| 379 | | |
|---|
| 380 | | /** |
|---|
| 381 | | * Get map size. |
|---|
| 382 | | * |
|---|
| 383 | | * @return Array of [width, height]. |
|---|
| 384 | | */ |
|---|
| 385 | | public function getSize():/*Number*/Array |
|---|
| 386 | | { |
|---|
| 387 | | var size:/*Number*/Array = [__width, __height]; |
|---|
| 388 | | return size; |
|---|
| 389 | | } |
|---|
| | 348 | * Return the current zoom level of the map. |
|---|
| | 349 | * |
|---|
| | 350 | * @return zoom number |
|---|
| | 351 | */ |
|---|
| | 352 | public function getZoom():int |
|---|
| | 353 | { |
|---|
| | 354 | return grid.zoomLevel; |
|---|
| | 355 | } |
|---|
| | 356 | |
|---|
| | 357 | |
|---|
| | 358 | /** |
|---|
| | 359 | * Set new map size, call onResized(). |
|---|
| | 360 | * |
|---|
| | 361 | * @param New map width. |
|---|
| | 362 | * @param New map height. |
|---|
| | 363 | * |
|---|
| | 364 | * @see com.modestmaps.Map#onResized |
|---|
| | 365 | */ |
|---|
| | 366 | public function setSize(width:Number, height:Number):void |
|---|
| | 367 | { |
|---|
| | 368 | __width = width; |
|---|
| | 369 | __height = height; |
|---|
| | 370 | if (grid) |
|---|
| | 371 | { |
|---|
| | 372 | grid.resizeTo(new Point(__width, __height)); |
|---|
| | 373 | } |
|---|
| | 374 | onResized(); |
|---|
| | 375 | |
|---|
| | 376 | // mask out out of bounds marker remnants |
|---|
| | 377 | scrollRect = new Rectangle(0,0,width,height); |
|---|
| | 378 | } |
|---|
| | 379 | |
|---|
| | 380 | /** |
|---|
| | 381 | * Get map size. |
|---|
| | 382 | * |
|---|
| | 383 | * @return Array of [width, height]. |
|---|
| | 384 | */ |
|---|
| | 385 | public function getSize():/*Number*/Array |
|---|
| | 386 | { |
|---|
| | 387 | var size:/*Number*/Array = [__width, __height]; |
|---|
| | 388 | return size; |
|---|
| | 389 | } |
|---|
| 398 | | public function getHeight():Number |
|---|
| 399 | | { |
|---|
| 400 | | return __height; |
|---|
| 401 | | } |
|---|
| 402 | | |
|---|
| 403 | | /** |
|---|
| 404 | | * Get a reference to the current map provider. |
|---|
| 405 | | * |
|---|
| 406 | | * @return Map provider. |
|---|
| 407 | | * |
|---|
| 408 | | * @see com.modestmaps.mapproviders.IMapProvider |
|---|
| 409 | | */ |
|---|
| 410 | | public function getMapProvider():IMapProvider |
|---|
| 411 | | { |
|---|
| 412 | | return __mapProvider; |
|---|
| 413 | | } |
|---|
| 414 | | |
|---|
| 415 | | /** |
|---|
| 416 | | * Set a new map provider, repainting tiles and changing bounding box if necessary. |
|---|
| 417 | | * |
|---|
| 418 | | * @param Map provider. |
|---|
| 419 | | * |
|---|
| 420 | | * @see com.modestmaps.mapproviders.IMapProvider |
|---|
| 421 | | */ |
|---|
| 422 | | public function setMapProvider(newProvider:IMapProvider):void |
|---|
| 423 | | { |
|---|
| 424 | | var previousGeometry:String; |
|---|
| 425 | | if (__mapProvider) |
|---|
| 426 | | { |
|---|
| 427 | | previousGeometry = __mapProvider.geometry(); |
|---|
| 428 | | } |
|---|
| 429 | | var extent:MapExtent = getExtent(); |
|---|
| 430 | | |
|---|
| 431 | | __mapProvider = newProvider; |
|---|
| 432 | | if (grid) |
|---|
| 433 | | { |
|---|
| 434 | | grid.setMapProvider(__mapProvider); |
|---|
| 435 | | } |
|---|
| 436 | | |
|---|
| 437 | | if (__mapProvider.geometry() == previousGeometry) |
|---|
| | 398 | public function getHeight():Number |
|---|
| | 399 | { |
|---|
| | 400 | return __height; |
|---|
| | 401 | } |
|---|
| | 402 | |
|---|
| | 403 | /** |
|---|
| | 404 | * Get a reference to the current map provider. |
|---|
| | 405 | * |
|---|
| | 406 | * @return Map provider. |
|---|
| | 407 | * |
|---|
| | 408 | * @see com.modestmaps.mapproviders.IMapProvider |
|---|
| | 409 | */ |
|---|
| | 410 | public function getMapProvider():IMapProvider |
|---|
| | 411 | { |
|---|
| | 412 | return __mapProvider; |
|---|
| | 413 | } |
|---|
| | 414 | |
|---|
| | 415 | /** |
|---|
| | 416 | * Set a new map provider, repainting tiles and changing bounding box if necessary. |
|---|
| | 417 | * |
|---|
| | 418 | * @param Map provider. |
|---|
| | 419 | * |
|---|
| | 420 | * @see com.modestmaps.mapproviders.IMapProvider |
|---|
| | 421 | */ |
|---|
| | 422 | public function setMapProvider(newProvider:IMapProvider):void |
|---|
| | 423 | { |
|---|
| | 424 | var previousGeometry:String; |
|---|
| | 425 | if (__mapProvider) |
|---|
| | 426 | { |
|---|
| | 427 | previousGeometry = __mapProvider.geometry(); |
|---|
| | 428 | } |
|---|
| | 429 | var extent:MapExtent = getExtent(); |
|---|
| | 430 | |
|---|
| | 431 | __mapProvider = newProvider; |
|---|
| | 432 | if (grid) |
|---|
| | 433 | { |
|---|
| | 434 | grid.setMapProvider(__mapProvider); |
|---|
| | 435 | } |
|---|
| | 436 | |
|---|
| | 437 | if (__mapProvider.geometry() == previousGeometry) |
|---|