Changeset 332
- Timestamp:
- 08/10/07 22:03:10 (1 year ago)
- Files:
-
- trunk/as3/lib/com/modestmaps/core/MarkerClip.as (modified) (6 diffs)
- trunk/as3/lib/com/modestmaps/Map.as (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/as3/lib/com/modestmaps/core/MarkerClip.as
r331 r332 13 13 import flash.geom.Point; 14 14 import com.modestmaps.events.MarkerEvent; 15 import flash.events.Event; 16 import flash.utils.Dictionary; 17 import flash.display.DisplayObject; 15 18 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 */ 16 24 public class MarkerClip extends Sprite 17 25 { … … 19 27 private var map:Map; 20 28 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 = {}; 22 32 23 33 public function MarkerClip(map:Map) … … 26 36 map.addEventListener(MarkerEvent.ENTER, onMapMarkerEnters); 27 37 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); 31 41 map.addEventListener(MapEvent.START_PANNING, onMapStartPanning); 32 42 map.addEventListener(MapEvent.STOP_PANNING, onMapStopPanning); 33 43 map.addEventListener(MapEvent.PANNED, onMapPanned); 34 44 map.addEventListener(MapEvent.RESIZED, onMapResized); 35 map.addEventListener(MapEvent.EXTENT_CHANGED, onMapExtentChanged);45 map.addEventListener(MapEvent.EXTENT_CHANGED, updateClips); 36 46 } 37 47 38 public function attachMarker( id:String, location:Location):Sprite48 public function attachMarker(marker:DisplayObject, location:Location):void 39 49 { 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); 44 53 45 54 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; 48 57 49 return sprite; 58 // TODO: check if it should be added now? 59 addChild(marker); 50 60 } 51 61 52 public function getMarker(id:String): Sprite62 public function getMarker(id:String):DisplayObject 53 63 { 54 return getChildByName(id) as Sprite;64 return markersByName[id] as DisplayObject; 55 65 } 56 66 57 67 public function removeMarker(id:String):void 58 68 { 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]; 60 76 } 61 77 62 private function updateClips( ):void78 private function updateClips(event:Event=null):void 63 79 { 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); 67 82 } 68 83 } 69 84 70 private function updateClip( sprite:Sprite):void85 private function updateClip(marker:DisplayObject):void 71 86 { 72 var location:Location = locations[ sprite.name];87 var location:Location = locations[marker]; 73 88 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; 76 91 } 77 92 78 93 public function onMapMarkerEnters(event:MarkerEvent):void 79 94 { 80 getChildByName(event.marker).visible = true; 95 /* if (!getChildByName(event.marker)) { 96 addChild(getMarker(event.marker)); 97 } */ 81 98 } 82 99 83 100 public function onMapMarkerLeaves(event:MarkerEvent):void 84 101 { 85 getChildByName(event.marker).visible = false; 102 /* if (getChildByName(event.marker)) { 103 removeChild(getMarker(event.marker)); 104 } */ 86 105 } 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 103 107 public function onMapStartPanning(event:MapEvent):void 104 108 { … … 106 110 } 107 111 108 public function onMapPanned( delta:Point):void112 public function onMapPanned(event:MapEvent):void 109 113 { 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; 112 116 } 113 117 … … 116 120 x = starting.x; 117 121 y = starting.y; 118 119 122 updateClips(); 120 123 } … … 124 127 x = event.newSize[0]/2; 125 128 y = event.newSize[1]/2; 126 127 129 updateClips(); 128 130 } 129 131 130 public function onMapExtentChanged(event:MapEvent):void131 {132 updateClips();133 }134 132 } 135 133 trunk/as3/lib/com/modestmaps/Map.as
r331 r332 39 39 import flash.external.ExternalInterface; 40 40 import flash.events.Event; 41 import flash.display.DisplayObject; 41 42 42 43 public class Map extends Sprite … … 117 118 118 119 markers = new MarkerClip(this); 120 markers.x = __width/2; 121 markers.y = __height/2; 119 122 addChild(markers); 120 123 … … 630 633 631 634 /** 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) 634 636 * 635 637 * @param ID of marker, opaque string. 636 638 * @param Location of marker. 637 * @param optionally return a spritethat will always be in the right place638 */ 639 public function putMarker(id:String, location:Location, ma keASprite:Boolean):Sprite639 * @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 640 642 { 641 643 //trace('Marker '+id+': '+location.toString()); 642 644 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 } 648 649 } 649 650 650 651 /** 651 * Get a marker clipwith the given id if one was created.652 * Get a marker with the given id if one was created. 652 653 * 653 654 * @param ID of marker, opaque string. 654 655 */ 655 public function getMarker(id:String): Sprite656 public function getMarker(id:String):DisplayObject 656 657 { 657 658 return markers.getMarker(id);
