Changeset 670

Show
Ignore:
Timestamp:
09/06/08 18:35:00 (3 months ago)
Author:
tom
Message:

fixed bug in locationsCoordinate (fitHeight was being ignored)
disabled double click response if map isn't draggable
added experimental mousewheel handler to TweenMap? (still too gloopy though)
minor cosmetic cleanups and todo notes in copyright handlers
new faster PolygonMarker/PolygonClip? modifications
removed MapExtent?.fromLocationArray (dupe of MapExtent?.fromLocations)
added MapExtent?.fromLocationProperties
made MarkerClip?.updateMarker useful if you've messed with yourMarker.visible (needs demo)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/as3/lib/com/modestmaps/core/MapExtent.as

    r647 r670  
    150150                } 
    151151 
    152         public static function fromLocations(locations:Array):MapExtent 
    153         { 
    154             var minLat:Number = Number.POSITIVE_INFINITY; 
    155             var minLon:Number = Number.POSITIVE_INFINITY; 
    156             var maxLat:Number = Number.NEGATIVE_INFINITY; 
    157             var maxLon:Number = Number.NEGATIVE_INFINITY; 
    158             for each (var location:Location in locations) 
    159             { 
    160                 if (!location) continue; 
    161                 minLat = Math.min(minLat, location.lat); 
    162                 maxLat = Math.max(maxLat, location.lat); 
    163                 minLon = Math.min(minLon, location.lon); 
    164                 maxLon = Math.max(maxLon, location.lon); 
    165             } 
    166             return new MapExtent(maxLat, minLat, maxLon, minLon); 
    167         } 
    168                          
    169         // TODO: decide which of fromLocationArray and fromLocations gets to stay! 
    170                 public static function fromLocationArray(locations:Array):MapExtent 
     152        /** calculate the north/south/east/west extremes of the given array of locations */ 
     153                public static function fromLocations(locations:Array):MapExtent 
    171154                { 
    172155                        if (!locations || locations.length == 0) return new MapExtent(); 
     
    174157                        var first:Location = locations[0] as Location; 
    175158                        var extent:MapExtent = new MapExtent(first.lat, first.lat, first.lon, first.lon); 
    176                         for (var i:int = 1; i < locations.length; i++
     159                        for each (var location:Location in locations.slice(1)
    177160                        { 
    178                                 extent.enclose(locations[i]); 
     161                                if (location) { 
     162                                        extent.enclose(location); 
     163                                } 
    179164                        } 
    180165                        return extent; 
    181166                } 
    182167                 
     168                public static function fromLocationProperties(objects:Array, locationProp:String='location'):MapExtent 
     169                { 
     170                        return fromLocations(objects.map(function(obj:Object, ...rest):Location { return obj[locationProp] as Location })); 
     171                } 
     172                 
    183173        } 
    184174} 
  • trunk/as3/lib/com/modestmaps/core/MarkerClip.as

    r647 r670  
    204204 
    205205                //var t:int = flash.utils.getTimer(); 
    206                 var w:Number = map.getWidth() * 2; 
    207                 var h:Number = map.getHeight() * 2; 
    208206                var doSort:Boolean = false; 
    209207                for each (marker in markers) 
    210208                { 
    211                     // TODO: note, hidden markers are not updated, so when  
    212                     // revealing markers using visible=true, they may end up in the wrong spot ? 
    213                     if (marker.visible) 
    214                     { 
    215                         updateClip(marker); 
    216                         if (markerInBounds(marker, w, h)) 
    217                         { 
    218                             if (!contains(marker)) 
    219                             { 
    220                                 addChild(marker); 
    221                                 doSort = true; 
    222                             } 
    223                         } 
    224                         else if (contains(marker)) 
    225                         { 
    226                             removeChild(marker); 
    227                             doSort = true; 
    228                         } 
    229                     } 
     209                    doSort = updateClip(marker) || doSort; // wow! bad things did happen when this said doSort ||= updateClip(marker); 
    230210                } 
    231211 
     
    266246            } 
    267247 
    268             public function updateClip(marker:DisplayObject):void 
    269             { 
    270                 // this method previously used the location of the marker 
    271                 // but map.locationPoint hands off to grid to grid.coordinatePoint 
    272                 // in the end so we may as well cache the first step 
    273                 var point:Point = map.grid.coordinatePoint(coordinates[marker], this); 
    274             marker.x = snapToPixels ? Math.round(point.x) : point.x; 
    275             marker.y = snapToPixels ? Math.round(point.y) : point.y; 
     248                /** returns true if the marker was added or removed from the stage, so that updateClips can sort the markers */  
     249            public function updateClip(marker:DisplayObject):Boolean 
     250            {            
     251            if (marker.visible) 
     252            { 
     253                        // this method previously used the location of the marker 
     254                        // but map.locationPoint hands off to grid to grid.coordinatePoint 
     255                        // in the end so we may as well cache the first step 
     256                        var point:Point = map.grid.coordinatePoint(coordinates[marker], this); 
     257                    marker.x = snapToPixels ? Math.round(point.x) : point.x; 
     258                    marker.y = snapToPixels ? Math.round(point.y) : point.y; 
     259 
     260                        var w:Number = map.getWidth() * 2; 
     261                        var h:Number = map.getHeight() * 2; 
     262                     
     263                if (markerInBounds(marker, w, h)) 
     264                { 
     265                    if (!contains(marker)) 
     266                    { 
     267                        addChild(marker); 
     268                        return true; 
     269                    } 
     270                } 
     271                else if (contains(marker)) 
     272                { 
     273                    removeChild(marker); 
     274                    return true; 
     275                } 
     276            } 
     277             
     278            return false;             
    276279            } 
    277280 
  • trunk/as3/lib/com/modestmaps/core/PolygonClip.as

    r647 r670  
    1111                { 
    1212                        super(map); 
    13                         this.scaleZoom = true; 
     13                        //this.scaleZoom = true; 
    1414                        this.markerSortFunction = null 
    1515                } 
     
    1717                override protected function markerInBounds(marker:DisplayObject, w:Number, h:Number):Boolean 
    1818                { 
    19                         return true; 
    20 /*                      var rect:Rectangle = new Rectangle(-map.width/2, -map.height/2, map.width, map.height); 
    21                         return rect.intersects(marker.getBounds(this)); */ 
     19                        var rect:Rectangle = new Rectangle(-w, -h, w*2, h*2); 
     20                        return rect.intersects(marker.getBounds(this)); 
    2221                }                
    2322                 
  • trunk/as3/lib/com/modestmaps/core/PolygonMarker.as

    r647 r670  
    1212        { 
    1313                protected var map:Map; 
     14                protected var drawZoom:Number; 
    1415                 
    15                 public var coordinates:Array = []; 
     16                public var locations:Array; 
     17                public var extent:MapExtent; 
    1618                public var location:Location; 
    1719                                 
     
    2527                public var fillAlpha:Number = 0.2; 
    2628                                 
    27                 public function PolygonMarker(map:Map, coordinates:Array) 
     29                public function PolygonMarker(map:Map, locations:Array) 
    2830                { 
    2931                        this.map = map; 
    3032                        this.mouseEnabled = false; 
    3133 
    32                         map.addEventListener(MapEvent.EXTENT_CHANGED, redraw); 
     34                        map.addEventListener(MapEvent.EXTENT_CHANGED, rescale); 
     35                        map.addEventListener(MapEvent.ZOOMED_BY, rescale); 
    3336                        map.addEventListener(MapEvent.STOP_ZOOMING, redraw); 
    3437                         
    35                         this.coordinates = coordinates;  
    36                         location = coordinates[0] as Location;           
     38                        if (locations && locations.length > 0) { 
     39                                this.locations = locations; 
     40                                this.extent = MapExtent.fromLocations(locations); 
     41                                this.location = locations[0] as Location; 
     42                        } 
     43                } 
     44         
     45                public function rescale(event:Event=null):void 
     46                { 
     47                        scaleX = scaleY = Math.pow(2, map.grid.zoomLevel - drawZoom); 
    3748                } 
    3849                 
    3950                public function redraw(event:Event=null):void 
    4051                { 
     52                        drawZoom = map.grid.zoomLevel; 
     53                        scaleX = scaleY = 1; 
     54                         
    4155                        var firstPoint:Point = map.locationPoint(location)               
    4256                        graphics.clear(); 
     
    5165                        } 
    5266                        graphics.moveTo(0, 0); 
    53                         for each (var loc:Location in coordinates.slice(1)) { 
     67                        for each (var loc:Location in locations.slice(1)) { 
    5468                                var p:Point = map.locationPoint(loc); 
    5569                                graphics.lineTo(p.x-firstPoint.x, p.y-firstPoint.y); 
  • trunk/as3/lib/com/modestmaps/extras/MapCopyright.as

    r647 r670  
    7878                                                            this.google.copyright('&t=h', cenLat, cenLon, maxLat-minLat, maxLon-minLon, zoom); 
    7979                                                            break; 
     80 
     81                                                                        // TODO: GOOGLE_TERRAIN 
    8082 
    8183                                                        case 'YAHOO_ROAD': 
  • trunk/as3/lib/com/modestmaps/extras/VirtualEarthCopyright.as

    r647 r670  
    2020                public var offsetY:Number = 10; 
    2121                 
    22                 public function VirtualEarthCopyright(map:Map, offsetX:Number=undefined, offsetY:Number=undefined):void 
     22                public function VirtualEarthCopyright(map:Map, offsetX:Number=undefined, offsetY:Number=undefined) 
    2323                {        
    2424                        super(map); 
  • trunk/as3/lib/com/modestmaps/Map.as

    r666 r670  
    220220                { 
    221221                        if (!fitWidth) fitWidth = mapWidth; 
    222                         if (!fitWidth) fitWidth = mapHeight; 
     222                        if (!fitHeight) fitHeight = mapHeight; 
    223223                         
    224224                var TL:Coordinate = mapProvider.locationCoordinate(locations[0]); 
     
    247247                 
    248248                // multiplication factor between vertical span and map height 
    249                 var vFactor:Number = (BR.row - TL.row) / (fitWidth / mapProvider.tileHeight); 
     249                var vFactor:Number = (BR.row - TL.row) / (fitHeight / mapProvider.tileHeight); 
    250250                 
    251251                // multiplication factor expressed as base-2 logarithm, for zoom difference 
     
    497497         
    498498                /** zoom in or out by zoomDelta, keeping the requested point in the same place */ 
    499         public function zoomByAbout(zoomDelta:int, targetPoint:Point=null, duration:Number=-1):void 
     499        public function zoomByAbout(zoomDelta:Number, targetPoint:Point=null, duration:Number=-1):void 
    500500        { 
    501501            if (!targetPoint) targetPoint = new Point(mapWidth/2, mapHeight/2);          
     
    649649            } 
    650650             
    651                 public function removeAllMakers():void { 
     651                public function removeAllMarkers():void { 
    652652                        markerClip.removeAllMarkers() 
    653653                } 
     
    693693        public function onDoubleClick(event:MouseEvent):void 
    694694        { 
     695                if (!__draggable) return; 
     696                 
    695697            var p:Point = grid.globalToLocal(new Point(event.stageX, event.stageY)); 
    696698            if (event.shiftKey) { 
     
    713715                } 
    714716            } 
    715         } 
     717        }         
    716718                 
    717719        } 
  • trunk/as3/lib/com/modestmaps/TweenMap.as

    r647 r670  
    1616        import com.modestmaps.mapproviders.IMapProvider; 
    1717         
     18        import flash.events.MouseEvent; 
    1819        import flash.geom.Matrix; 
    1920        import flash.geom.Point; 
     21        import flash.utils.clearTimeout; 
     22        import flash.utils.setTimeout; 
    2023         
    2124        import gs.TweenLite; 
     
    3639                /** time to pan and zoom using, uh, panAndZoom */ 
    3740                public var panAndZoomDuration:Number = 0.3; 
     41 
     42                protected var mouseWheelingIn:Boolean = false; 
     43                protected var mouseWheelingOut:Boolean = false; 
    3844 
    3945        /* 
     
    98104 
    99105                /** zoom in or out by zoomDelta, keeping the requested point in the same place */         
    100         override public function zoomByAbout(zoomDelta:int, targetPoint:Point=null, duration:Number=-1):void 
     106        override public function zoomByAbout(zoomDelta:Number, targetPoint:Point=null, duration:Number=-1):void 
    101107        { 
    102108            if (duration < 0) duration = panAndZoomDuration; 
    103109            if (!targetPoint) targetPoint = new Point(mapWidth/2, mapHeight/2);          
    104110 
    105                 if (grid.zoomLevel + zoomDelta < grid.minZoom) { 
    106                         zoomDelta = grid.minZoom - grid.zoomLevel;                       
    107                 } 
    108                 else if (grid.zoomLevel + zoomDelta > grid.maxZoom) { 
    109                         zoomDelta = grid.maxZoom - grid.zoomLevel;  
     111                        var constrainedDelta:Number = zoomDelta; 
     112 
     113                if (grid.zoomLevel + constrainedDelta < grid.minZoom) { 
     114                        constrainedDelta = grid.minZoom - grid.zoomLevel;                        
     115                } 
     116                else if (grid.zoomLevel + constrainedDelta > grid.maxZoom) { 
     117                        constrainedDelta = grid.maxZoom - grid.zoomLevel;  
    110118                } 
    111119                 
    112120                // round the zoom delta up or down so that we end up at a power of 2 
    113                 var preciseZoomDelta:Number = zoomDelta + (Math.round(grid.zoomLevel) - grid.zoomLevel) 
     121                var preciseZoomDelta:Number; 
     122 
     123                        if (mouseWheelingIn || mouseWheelingOut) { 
     124                                preciseZoomDelta = constrainedDelta; 
     125                        } 
     126                        else { 
     127                                preciseZoomDelta = constrainedDelta + (Math.round(grid.zoomLevel) - grid.zoomLevel); 
     128                        } 
    114129                 
    115130                var sc:Number = Math.pow(2, preciseZoomDelta); 
     
    224239            } 
    225240 
     241                protected var wheeltimer:uint; 
     242 
     243        /** zooms in or out of mouse-wheeled location */ 
     244        public function onMouseWheel(event:MouseEvent):void 
     245        {                
     246                if (!__draggable) return; 
     247 
     248            var p:Point = grid.globalToLocal(new Point(event.stageX, event.stageY)); 
     249            if (event.delta < 0) { 
     250                if (grid.zoomLevel > grid.minZoom) { 
     251                                mouseWheelingOut = true; 
     252                                mouseWheelingIn = false; 
     253                        zoomByAbout(Math.min(-1,event.delta/20.0), p, 0.0); 
     254                } 
     255                else { 
     256                        panBy(mapWidth/2 - p.x, mapHeight/2 - p.y); 
     257                } 
     258            } 
     259            else if (event.delta > 0) { 
     260                if (grid.zoomLevel < grid.maxZoom) { 
     261                        mouseWheelingIn = true; 
     262                                mouseWheelingOut = false;                        
     263                        zoomByAbout(Math.max(1,event.delta/20.0), p, 0.0); 
     264                    } 
     265                else { 
     266                        panBy(mapWidth/2 - p.x, mapHeight/2 - p.y); 
     267                } 
     268            } 
     269             
     270                if (wheeltimer) { 
     271                        clearTimeout(wheeltimer); 
     272                } 
     273                 
     274            if (mouseWheelingIn || mouseWheelingOut) { 
     275                                wheeltimer = setTimeout(doneMouseWheeling, 100); 
     276            } 
     277        } 
     278         
     279        protected function doneMouseWheeling():void 
     280        { 
     281                if (mouseWheelingIn) {  
     282                        zoomByAbout(Math.ceil(grid.zoomLevel) - grid.zoomLevel, new Point(grid.mouseX, grid.mouseY), 0.05); // round off to whole value up 
     283                } 
     284                else if (mouseWheelingOut) {  
     285                        zoomByAbout(Math.floor(grid.zoomLevel) - grid.zoomLevel, new Point(grid.mouseX, grid.mouseY), 0.05); // round off to whole value down 
     286                } 
     287                mouseWheelingOut = false; 
     288                mouseWheelingIn = false; 
     289        } 
     290         
    226291        } 
    227292}