Changeset 332

Show
Ignore:
Timestamp:
08/10/07 22:03:10 (1 year ago)
Author:
tom
Message:

really ported as2's MarkerClip? to as3, but now it works a bit differently

Files:

Legend:

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

    r331 r332  
    1313        import flash.geom.Point; 
    1414        import com.modestmaps.events.MarkerEvent; 
     15        import flash.events.Event; 
     16        import flash.utils.Dictionary; 
     17        import flash.display.DisplayObject; 
    1518         
     19        /** This is different from the as2 version for now, because 
     20         *  it makes more sense to me if you give it a Sprite  
     21         *  (or DisplayObject) to take care of rather than ask it to 
     22         *  make one for you. 
     23         */ 
    1624        public class MarkerClip extends Sprite 
    1725        { 
     
    1927            private var map:Map; 
    2028            private var starting:Point; 
    21             private var locations:Object = {}; 
     29            private var locations:Dictionary = new Dictionary(); 
     30            private var markers:Array = []; 
     31            private var markersByName:Object = {}; 
    2232         
    2333            public function MarkerClip(map:Map) 
     
    2636                map.addEventListener(MarkerEvent.ENTER, onMapMarkerEnters); 
    2737                map.addEventListener(MarkerEvent.LEAVE, onMapMarkerLeaves); 
    28                 map.addEventListener(MapEvent.START_ZOOMING, onMapStartZooming); 
    29                 map.addEventListener(MapEvent.STOP_ZOOMING, onMapStopZooming); 
    30                 map.addEventListener(MapEvent.ZOOMED_BY, onMapZoomed); 
     38                map.addEventListener(MapEvent.START_ZOOMING, updateClips); 
     39                map.addEventListener(MapEvent.STOP_ZOOMING, updateClips); 
     40                map.addEventListener(MapEvent.ZOOMED_BY, updateClips); 
    3141                map.addEventListener(MapEvent.START_PANNING, onMapStartPanning); 
    3242                map.addEventListener(MapEvent.STOP_PANNING, onMapStopPanning); 
    3343                map.addEventListener(MapEvent.PANNED, onMapPanned); 
    3444                map.addEventListener(MapEvent.RESIZED, onMapResized); 
    35                 map.addEventListener(MapEvent.EXTENT_CHANGED, onMapExtentChanged); 
     45                map.addEventListener(MapEvent.EXTENT_CHANGED, updateClips); 
    3646            } 
    3747             
    38             public function attachMarker(id:String, location:Location):Sprite 
     48            public function attachMarker(marker:DisplayObject, location:Location):void 
    3949            { 
    40                 var sprite:Sprite = addChild(new Sprite()) as Sprite; 
    41                 sprite.name = id; 
    42          
    43                 locations[id] = location; 
     50                locations[marker] = location; 
     51                markersByName[marker.name] = marker; 
     52                markers.push(marker); 
    4453                 
    4554                var point:Point = map.locationPoint(location, this); 
    46                 sprite.x = point.x; 
    47                 sprite.y = point.y; 
     55                marker.x = point.x; 
     56                marker.y = point.y; 
    4857                 
    49                 return sprite; 
     58                // TODO: check if it should be added now? 
     59                addChild(marker); 
    5060            } 
    5161             
    52             public function getMarker(id:String):Sprite 
     62            public function getMarker(id:String):DisplayObject 
    5363            { 
    54                 return getChildByName(id) as Sprite
     64                return markersByName[id] as DisplayObject
    5565            } 
    5666             
    5767            public function removeMarker(id:String):void 
    5868            { 
    59                 removeChild(getMarker(id)); 
     69                var marker:DisplayObject = getMarker(id); 
     70                if (this.getChildByName(id)) removeChild(marker); 
     71                var index:int = markers.indexOf(marker); 
     72                if (index >= 0) { 
     73                        markers.splice(index,1); 
     74                } 
     75                delete locations[marker]; 
    6076            } 
    6177                 
    62             private function updateClips():void 
     78            private function updateClips(event:Event=null):void 
    6379            { 
    64                 for (var i:int = 0; i < numChildren; i++) { 
    65                         var sprite:Sprite = getChildAt(0) as Sprite;     
    66                         updateClip(sprite); 
     80                for each (var marker:DisplayObject in markers) { 
     81                        updateClip(marker); 
    6782                } 
    6883            } 
    6984             
    70             private function updateClip(sprite:Sprite):void 
     85            private function updateClip(marker:DisplayObject):void 
    7186            { 
    72                 var location:Location = locations[sprite.name]; 
     87                var location:Location = locations[marker]; 
    7388                var point:Point = map.locationPoint(location,this); 
    74                 sprite.x = point.x; 
    75                 sprite.y = point.y; 
     89                marker.x = point.x; 
     90                marker.y = point.y; 
    7691            } 
    7792                     
    7893            public function onMapMarkerEnters(event:MarkerEvent):void 
    7994            { 
    80                 getChildByName(event.marker).visible = true; 
     95/*              if (!getChildByName(event.marker)) { 
     96                        addChild(getMarker(event.marker)); 
     97                } */ 
    8198            } 
    8299             
    83100            public function onMapMarkerLeaves(event:MarkerEvent):void 
    84101            { 
    85                 getChildByName(event.marker).visible = false; 
     102/*              if (getChildByName(event.marker)) { 
     103                        removeChild(getMarker(event.marker)); 
     104                } */ 
    86105            } 
    87              
    88             public function onMapStartZooming(event:MapEvent):void 
    89             { 
    90                 updateClips(); 
    91             } 
    92              
    93             public function onMapZoomed(event:MapEvent):void 
    94             { 
    95                 updateClips(); 
    96             } 
    97              
    98             public function onMapStopZooming(event:MapEvent):void 
    99             { 
    100                 updateClips(); 
    101             } 
    102              
     106                     
    103107            public function onMapStartPanning(event:MapEvent):void 
    104108            { 
     
    106110            } 
    107111             
    108             public function onMapPanned(delta:Point):void 
     112            public function onMapPanned(event:MapEvent):void 
    109113            { 
    110                 x = starting.x + delta.x; 
    111                 y = starting.y + delta.y; 
     114                x = starting.x + event.panDelta.x; 
     115                y = starting.y + event.panDelta.y; 
    112116            } 
    113117             
     
    116120                x = starting.x; 
    117121                y = starting.y; 
    118  
    119122                updateClips(); 
    120123            } 
     
    124127                x = event.newSize[0]/2; 
    125128                y = event.newSize[1]/2; 
    126          
    127129                updateClips(); 
    128130            } 
    129131             
    130             public function onMapExtentChanged(event:MapEvent):void 
    131             { 
    132                 updateClips(); 
    133             } 
    134132        } 
    135133         
  • trunk/as3/lib/com/modestmaps/Map.as

    r331 r332  
    3939        import flash.external.ExternalInterface; 
    4040        import flash.events.Event; 
     41        import flash.display.DisplayObject; 
    4142         
    4243        public class Map extends Sprite 
     
    117118 
    118119                        markers = new MarkerClip(this); 
     120                        markers.x = __width/2; 
     121                        markers.y = __height/2; 
    119122                        addChild(markers); 
    120123 
     
    630633         
    631634           /** 
    632             * Add a marker with the given id and location (lat, lon) 
    633             * optionally return a Sprite. 
     635            * Add a marker at the given location (lat, lon) 
    634636            * 
    635637            * @param    ID of marker, opaque string. 
    636638            * @param    Location of marker. 
    637             * @param    optionally return a sprite that will always be in the right place 
    638             */ 
    639             public function putMarker(id:String, location:Location, makeASprite:Boolean):Sprite 
     639            * @param    optionally, a sprite (where sprite.name=id) that will always be in the right place 
     640            */ 
     641            public function putMarker(id:String, location:Location, marker:DisplayObject=null):void 
    640642            { 
    641643                //trace('Marker '+id+': '+location.toString()); 
    642644                grid.putMarker(id, __mapProvider.locationCoordinate(location), location); 
    643                  
    644                 if (makeASprite) { 
    645                         return markers.attachMarker(id, location); 
    646                 } 
    647                 return undefined; 
     645                if (marker) { 
     646                        //if (marker.name != id) throw new Error("marker name must match id"); 
     647                        markers.attachMarker(marker, location); 
     648                } 
    648649            } 
    649650 
    650651                /** 
    651                  * Get a marker clip with the given id if one was created. 
     652                 * Get a marker with the given id if one was created. 
    652653                 * 
    653654                 * @param    ID of marker, opaque string. 
    654655                 */ 
    655                 public function getMarker(id:String):Sprite 
     656                public function getMarker(id:String):DisplayObject 
    656657                { 
    657658                        return markers.getMarker(id);