PageRenderTime 45ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/google/maps/extras/markerclusterer/ClusterMarker.as

http://gmaps-utility-library-flash.googlecode.com/
ActionScript | 241 lines | 100 code | 35 blank | 106 comment | 4 complexity | 82c2ce9167a2e042dc39ff5d7336ce43 MD5 | raw file
  1. /**
  2. * @name MarkerClusterer for Flash
  3. * @version 1.0
  4. * @author Xiaoxi Wu
  5. * @copyright (c) 2009 Xiaoxi Wu
  6. * @fileoverview
  7. * Ported from Javascript to Actionscript 3 by Sean Toru
  8. * Ported for use in Flex (removal of fl. libraries) by Ian Watkins
  9. * Reflectored for both Flash and Flex,
  10. * and maintained by Juguang XIAO (juguang@gmail.com)
  11. *
  12. * This actionscript library creates and manages per-zoom-level
  13. * clusters for large amounts of markers (hundreds or thousands).
  14. * This library was inspired by the <a href="http://www.maptimize.com">
  15. * Maptimize</a> hosted clustering solution.
  16. * <br /><br/>
  17. * <b>How it works</b>:<br/>
  18. * The <code>MarkerClusterer</code> will group markers into clusters according to
  19. * their distance from a cluster's center. When a marker is added,
  20. * the marker cluster will find a position in all the clusters, and
  21. * if it fails to find one, it will create a new cluster with the marker.
  22. * The number of markers in a cluster will be displayed
  23. * on the cluster marker. When the map viewport changes,
  24. * <code>MarkerClusterer</code> will destroy the clusters in the viewport
  25. * and regroup them into new clusters.
  26. *
  27. */
  28. /*
  29. * Licensed under the Apache License, Version 2.0 (the "License");
  30. * you may not use this file except in compliance with the License.
  31. * You may obtain a copy of the License at
  32. *
  33. * http://www.apache.org/licenses/LICENSE-2.0
  34. *
  35. * Unless required by applicable law or agreed to in writing, software
  36. * distributed under the License is distributed on an "AS IS" BASIS,
  37. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  38. * See the License for the specific language governing permissions and
  39. * limitations under the License.
  40. */
  41. package com.google.maps.extras.markerclusterer
  42. {
  43. import com.google.maps.LatLng;
  44. import com.google.maps.MapEvent;
  45. import com.google.maps.interfaces.IMap;
  46. import com.google.maps.interfaces.IPane;
  47. import com.google.maps.interfaces.IPaneManager;
  48. import com.google.maps.overlays.OverlayBase;
  49. import flash.display.Bitmap;
  50. import flash.display.Loader;
  51. import flash.events.Event;
  52. import flash.geom.Point;
  53. import flash.net.URLRequest;
  54. import flash.text.TextField;
  55. import flash.text.TextFormat;
  56. import flash.text.TextFormatAlign;
  57. /**
  58. *
  59. * This is a presentation class for <code>Cluster</code>.
  60. * The current implementation is to load images for different level.
  61. * Later, it can be implemented with other possibilities.
  62. *
  63. *
  64. * Update:
  65. * 2010-02-11
  66. * Now it works with Flash, that means it also works for Flex.
  67. * 2010-02-13
  68. * Make it formal as overlay .e.g. create and destroy content as needed -
  69. * through MapEvent.OVERLAY_ADDED, and MapEvent.OVERLAY_REMOVED events.
  70. */
  71. internal class ClusterMarker extends OverlayBase
  72. {
  73. private var _latlng : LatLng;
  74. private var _count : int;
  75. private var _styles : Array;
  76. // private var text_ : String;
  77. // private var padding_ : Number;
  78. // private var imageLoader : Image;
  79. // private var label : Label;
  80. public function ClusterMarker (latlng : LatLng, count:int , styles : Array, padding : Number)
  81. {
  82. _latlng = latlng;
  83. _styles = styles;
  84. _count = count;
  85. this.addEventListener(MapEvent.OVERLAY_ADDED, onOverlayAdded);
  86. this.addEventListener(MapEvent.OVERLAY_REMOVED, onOverlayRemoved);
  87. }
  88. private var _ld:Loader;
  89. private var _tf:TextField;
  90. private function onOverlayAdded(event:MapEvent):void{
  91. var index : int = 0;
  92. var dv : int = _count;
  93. while (dv != 0) {
  94. dv = dv / 10;
  95. index ++;
  96. }
  97. if (_styles.length < index) {
  98. index = _styles.length;
  99. }
  100. var url_ :String = _styles[index - 1].url;
  101. // textColor_ = styles[index - 1].opt_textColor;
  102. // anchor_ = styles[index - 1].opt_anchor;
  103. var text_:String = String(_count);
  104. // padding_ = padding;
  105. /*
  106. imageLoader = new Image();
  107. imageLoader.autoLoad = true;
  108. imageLoader.source = url_;
  109. imageLoader.scaleContent = false;
  110. imageLoader.addEventListener(Event.COMPLETE, completeHandler);
  111. addChild(imageLoader);
  112. */
  113. var req:URLRequest = new URLRequest(url_);
  114. _ld = new Loader;
  115. _ld.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  116. _ld.load(req);
  117. this.addChild(_ld);
  118. /*
  119. @20100211
  120. Previous code, for Flex only!
  121. label = new Label();
  122. label.setStyle("textAlign", "center");
  123. label.setStyle("color", "0x00FF00");
  124. label.setStyle("font", "Verdana");
  125. label.setStyle("bold", "true");
  126. label.selectable = false;
  127. label.text = text_;
  128. addChild(label);
  129. */
  130. var format :TextFormat = new TextFormat;
  131. format.align = TextFormatAlign.CENTER;
  132. format.color = 0xFFFFFF;
  133. format.font = 'Verdana';
  134. format.bold = true;
  135. this._tf = new TextField;
  136. _tf.selectable = false;
  137. _tf.text = text_;
  138. _tf.setTextFormat(format);
  139. this.addChild(_tf);
  140. }
  141. private function onOverlayRemoved(event:MapEvent):void{
  142. this.removeChild(_ld);
  143. _ld = null;
  144. this.removeChild(_tf);
  145. _tf = null;
  146. }
  147. private function completeHandler(event : Event) : void
  148. {
  149. // imageLoader.width = event.currentTarget.content.width;
  150. // imageLoader.height = event.currentTarget.content.height;
  151. var content:Bitmap = event.currentTarget.content;
  152. _ld.width = content.width;
  153. _ld.height = content.height;
  154. // event.currentTarget.move(imageLoader.width/-2, imageLoader.height/-2);
  155. _ld.x = _ld.width / -2;
  156. _ld.y = _ld.height / -2;
  157. /* label.width = imageLoader.width;
  158. label.height = 14;
  159. label.x = imageLoader.width / -2;
  160. label.y = 0 - (label.height / 2);
  161. label.visible = true; */
  162. this._tf.width = _ld.width;
  163. this._tf.height = 16;
  164. this._tf.x = - _ld.width / 2;
  165. this._tf.y = 0 - (_tf.height/2);
  166. }
  167. override public function getDefaultPane(map : IMap): IPane
  168. {
  169. var paneManager : IPaneManager = map.getPaneManager();
  170. // return paneManager.getPaneById(PaneId.PANE_MARKER);
  171. return null;
  172. }
  173. override public function positionOverlay(zoomChanged : Boolean): void
  174. {
  175. this.reposition();
  176. }
  177. /* private function remove () : void
  178. {
  179. parent.removeChild(this);
  180. } */
  181. /*
  182. private function copy () : ClusterMarker
  183. {
  184. return new ClusterMarker(this.latlng_, this.index_, this.styles_, this.padding_);
  185. }
  186. */
  187. /**
  188. *
  189. * Developer should not call this function.
  190. * It is managed by Cluster.
  191. */
  192. internal function redraw (force:Boolean) : void
  193. {
  194. if (!force){
  195. return;
  196. }
  197. this.reposition();
  198. }
  199. private function reposition():void{
  200. var pos : Point = this.pane.fromLatLngToPaneCoords(_latlng);
  201. x = pos.x;
  202. y = pos.y;
  203. }
  204. }
  205. }