root/trunk/as2/lib/com/modestmaps/core/MarkerSet.as

Revision 292, 3.1 kB (checked in by migurski, 1 year ago)

Added Id keywords all over the place

  • Property svn:keywords set to Id
Line 
1 /*
2  * vim:et sts=4 sw=4 cindent:
3  * $Id$
4  */
5
6 import com.modestmaps.core.Coordinate;
7 import com.modestmaps.core.Marker;
8 import com.modestmaps.core.Tile;
9 import com.modestmaps.core.TileGrid;
10
11 class com.modestmaps.core.MarkerSet
12 {
13     private var __lastZoom:Number;
14    
15     // markers hashed by id
16     private var __markers:Object;
17    
18     // marker lists hashed by containing tile id
19     private var __tileMarkers:Object;
20    
21     // tile id's hashed by marker id
22     private var __markerTiles:Object;
23    
24     // for finding which is visible
25     private var __grid:TileGrid;
26
27     function MarkerSet(grid:TileGrid)
28     {
29         __grid = grid;
30         initializeIndex();
31     }
32    
33     /**
34      * Put a marker on the grid.
35      */
36     public function put(marker:Marker):Void
37     {
38         __markers[marker.id] = marker;
39         indexMarker(marker.id);
40     }
41    
42     /**
43      * Remove a marker added via put().
44      */
45     public function remove(marker:Marker):Void
46     {
47         unIndexMarker(marker.id);
48         delete __markers[marker.id];
49     }
50
51     public function initializeIndex():Void
52     {
53         __lastZoom = 0;
54
55         __markers = {};
56         __tileMarkers = {};
57         __markerTiles = {};
58     }
59
60     public function indexAtZoom(level:Number):Void
61     {
62         __lastZoom = level;
63    
64         for(var markerId:String in __markers)
65             indexMarker(markerId);
66     }
67
68    /**
69     * Add a new marker to the internal index.
70     */
71     private function indexMarker(markerId:String):Void
72     {
73         var tileKey:String = __markers[markerId].coord.zoomTo(__lastZoom).container().toString();
74        
75         if(__tileMarkers[tileKey] == undefined)
76             __tileMarkers[tileKey] = {};
77            
78         __tileMarkers[tileKey][markerId] = true;
79        
80         if(__markerTiles[markerId] == undefined)
81             __markerTiles[markerId] = {};
82            
83         __markerTiles[markerId][tileKey] = true;
84        
85         //trace('Marker '+markerId+' in '+tileKey);
86     }
87
88     /**
89      * Remove a marker from the internal index.
90      */
91     private function unIndexMarker(markerId:String):Void
92     {
93         for(var tileKey:String in __markerTiles[markerId])
94             delete __tileMarkers[tileKey][markerId];
95
96         delete __markerTiles[markerId];
97     }
98
99    /**
100     * Fetch a single marker by ID.
101     */
102     public function getMarker(id:String):Marker
103     {
104         return __markers[id];
105     }
106
107    /**
108     * Fetch a list of markers within currently-visible tiles.
109     */
110     public function overlapping(tiles:/*Tile*/Array):/*Marker*/Array
111     {
112         var ids:Array = [];
113         var touched:/*Marker*/Array = [];
114         var sourceCoord:Coordinate;
115        
116         for(var i:Number = 0; i < tiles.length; i += 1) {
117             sourceCoord = __grid.getMapProvider().sourceCoordinate(tiles[i].getCoord());
118        
119             if(__tileMarkers[sourceCoord.toString()] != undefined)
120                 for(var markerId:String in __tileMarkers[sourceCoord.toString()]) {
121                     ids.push(markerId);
122                     touched.push(__markers[markerId]);
123                 }
124         }
125        
126         //trace('Touched markers: '+ids.toString());
127         return touched;
128     }
129 }
Note: See TracBrowser for help on using the browser.