PageRenderTime 16ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/StyledKMLParser/src/KMLParser.mxml

http://gmaps-utility-library-flash.googlecode.com/
Macromedia eXtensible Markup Language | 233 lines | 214 code | 19 blank | 0 comment | 0 complexity | a6ed0a945b12fba099a19a8957246572 MD5 | raw file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. Copyright 2008 Google Inc.
  4. Licensed under the Apache License, Version 2.0:
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. -->
  7. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:maps="com.google.maps.*" layout="absolute" width="100%" height="100%" viewSourceURL="srcview/index.html">
  8. <mx:Panel title="Google Maps API for Flash Demo" width="100%" height="100%">
  9. <mx:VBox width="100%" height="100%">
  10. <mx:ComboBox
  11. id="kmlFiles"
  12. dataProvider = "{SAMPLE_FILES}"
  13. change="loadFile(event)"/>
  14. <mx:HDividedBox width="100%" height="100%">
  15. <mx:Tree id="kmlTree" width="200" height="100%" dataProvider="{kmlObj}" change="changeEvt(event);" labelField="name" showRoot="false"/>
  16. <maps:Map
  17. id="map"
  18. key="ABQIAAAA7QUChpcnvnmXxsjC7s1fCxQGj0PqsCtxKvarsoS-iqLdqZSKfxTd7Xf-2rEc_PC9o8IsJde80Wnj4g"
  19. mapevent_mapready="onMapReady(event)"
  20. width="100%" height="100%"/>
  21. </mx:HDividedBox>
  22. </mx:VBox>
  23. </mx:Panel>
  24. <mx:Script>
  25. <![CDATA[
  26. import flash.events.Event;
  27. import com.google.maps.controls.ZoomControl;
  28. import com.google.maps.overlays.Polyline;
  29. import com.google.maps.overlays.Polygon;
  30. import com.google.maps.overlays.PolygonOptions;
  31. import com.google.maps.overlays.Marker;
  32. import com.google.maps.MapEvent;
  33. import com.google.maps.Map;
  34. import com.google.maps.MapType;
  35. import com.google.maps.LatLng;
  36. import com.google.maps.LatLngBounds;
  37. import com.google.maps.overlays.GroundOverlay;
  38. import com.google.maps.overlays.GroundOverlayOptions;
  39. import com.google.maps.styles.*;
  40. import com.google.maps.extras.xmlparsers.kml.*;
  41. import mx.controls.Alert;
  42. [Bindable] private var kmlObj:Object = new Object();
  43. private var kmlStyles:Object = new Object();
  44. public var SAMPLE_FILES:Array = ["styledpoly.xml", "debug.xml", "multigeometry.xml", "polygon.xml"];
  45. public function onMapReady(event:MapEvent):void {
  46. map.setCenter(new LatLng(37.422289,-122.0822035), 14, MapType.NORMAL_MAP_TYPE);
  47. map.addControl(new ZoomControl());
  48. loadFile(null);
  49. }
  50. public function loadFile(event:Event):void {
  51. var file:String = kmlFiles.selectedLabel;
  52. var loader:URLLoader = new URLLoader();
  53. loader.addEventListener(Event.COMPLETE, loadXML);
  54. loader.load(new URLRequest(file));
  55. }
  56. public function loadXML(event:Event):void {
  57. var kml:Kml22 = new Kml22(event.target.data);
  58. var rootFeature:Feature = kml.feature;
  59. findStyle(rootFeature);
  60. // @todo: This is redundant with code in getChildrenFeatures, should combine
  61. kmlObj = new Object();
  62. kmlObj.name = rootFeature.name;
  63. kmlObj.mapObjs = new Array();
  64. kmlObj.bounds = new LatLngBounds();
  65. if (canContainFeatures(rootFeature)) {
  66. kmlObj.children = getChildrenFeatures(Container(rootFeature));
  67. } else {
  68. associateWithMapObject(kmlObj, rootFeature);
  69. }
  70. map.setCenter(kmlObj.bounds.getCenter(), map.getBoundsZoomLevel(kmlObj.bounds));
  71. }
  72. private function changeEvt(event:Event):void {
  73. var currentObj:Object = event.currentTarget.selectedItem;
  74. if (currentObj.bounds) { // Otherwise it's a folder
  75. map.setCenter(currentObj.bounds.getCenter(), map.getBoundsZoomLevel(currentObj.bounds));
  76. }
  77. }
  78. public function associateWithMapObject(obj:Object, feature:Feature):void {
  79. // at this point it can either be a placemark or a groundoverlay
  80. if (feature is Placemark) {
  81. var placemark:Placemark = Placemark(feature);
  82. if (placemark.geometry != null) {
  83. associateGeometryWithMapObject(obj, placemark.geometry, placemark.styleUrl);
  84. }
  85. } else if (feature is KmlGroundOverlay) {
  86. var groundOverlay:KmlGroundOverlay = KmlGroundOverlay(feature);
  87. var latLngBounds:LatLngBounds = new LatLngBounds(new LatLng(groundOverlay.latLonBox.south,groundOverlay.latLonBox.west), new LatLng(groundOverlay.latLonBox.north,groundOverlay.latLonBox.east));
  88. updateBounds(obj, latLngBounds);
  89. var testLoader:Loader = new Loader();
  90. var urlRequest:URLRequest = new URLRequest(groundOverlay.icon.href);
  91. testLoader.contentLoaderInfo.addEventListener(
  92. Event.COMPLETE,
  93. function(e:Event):void {
  94. obj.mapObject = new com.google.maps.overlays.GroundOverlay::GroundOverlay(testLoader, latLngBounds);
  95. map.addOverlay(obj.mapObject);
  96. });
  97. testLoader.load(urlRequest);
  98. }
  99. }
  100. public function associateGeometryWithMapObject(obj:Object, geometry:Geometry, styleUrl:String):void {
  101. if (geometry is KmlPoint) {
  102. var point:KmlPoint = KmlPoint(geometry);
  103. var latlng:LatLng = new LatLng(point.coordinates.coordsList[0].lat, point.coordinates.coordsList[0].lon);
  104. obj.mapObjs.push(new Marker(latlng));
  105. updateBounds(obj, new LatLngBounds(latlng, latlng));
  106. map.addOverlay(obj.mapObjs[obj.mapObjs.length -1]);
  107. } else if (geometry is LineString) {
  108. var lineString:LineString = LineString(geometry);
  109. var polyline:Polyline = new Polyline(getCoordinatesLatLngs(lineString.coordinates));
  110. obj.mapObjs.push(polyline);
  111. updateBounds(obj, polyline.getLatLngBounds());
  112. obj.center = polyline.getLatLngBounds().getCenter();
  113. obj.bounds = polyline.getLatLngBounds();
  114. map.addOverlay(polyline);
  115. } else if (geometry is LinearRing) {
  116. var linearRing:LinearRing = LinearRing(geometry);
  117. var polyline:Polyline = new Polyline(getCoordinatesLatLngs(linearRing.coordinates));
  118. obj.mapObjs.push(polyline);
  119. updateBounds(obj, polyline.getLatLngBounds());
  120. map.addOverlay(polyline);
  121. } else if (geometry is KmlPolygon) {
  122. var kmlPolygon:KmlPolygon = KmlPolygon(geometry);
  123. var options:PolygonOptions = new PolygonOptions();
  124. if (styleUrl) {
  125. styleUrl = styleUrl.replace("#", "");
  126. if (kmlStyles[styleUrl]) {
  127. var style:Style = kmlStyles[styleUrl];
  128. options.fillStyle = new FillStyle();
  129. options.fillStyle.color = style.polyStyle.color;
  130. options.strokeStyle = new StrokeStyle();
  131. options.strokeStyle.thickness = style.lineStyle.width;
  132. }
  133. }
  134. var polygon:Polygon = new Polygon(getCoordinatesLatLngs(kmlPolygon.outerBoundaryIs.linearRing.coordinates), options);
  135. obj.mapObjs.push(polygon);
  136. updateBounds(obj, polygon.getLatLngBounds());
  137. map.addOverlay(polygon);
  138. } else if (geometry is MultiGeometry) {
  139. var multiGeometry:MultiGeometry = MultiGeometry(geometry);
  140. for (var i:uint = 0; i < multiGeometry.geometries.length; i++) {
  141. associateGeometryWithMapObject(obj, multiGeometry.geometries[i], null);
  142. }
  143. }
  144. }
  145. public function updateBounds(obj:Object, bounds:LatLngBounds):void {
  146. if (obj.bounds) {
  147. obj.bounds.union(bounds);
  148. } else {
  149. obj.bounds = bounds;
  150. }
  151. kmlObj.bounds.union(bounds);
  152. }
  153. public function getCoordinatesLatLngs(coordinates:Coordinates):Array {
  154. var latlngs:Array = new Array();
  155. for (var i:Number = 0; i < coordinates.coordsList.length; i++) {
  156. var coordinate:Object = coordinates.coordsList[i];
  157. latlngs.push(new LatLng(Number(coordinate.lat), Number(coordinate.lon)));
  158. }
  159. return latlngs;
  160. }
  161. public function getChildrenFeatures(container:Container):Array {
  162. var childrenFeatures:Array = new Array();
  163. for (var i:Number = 0; i < container.features.length; i++) {
  164. var feature:Feature = container.features[i];
  165. var childObj:Object = new Object();
  166. childObj.mapObjs = new Array();
  167. childObj.name = feature.name;
  168. if (childObj.name == null) {
  169. childObj.name = getAlternateName(feature);
  170. }
  171. if (canContainFeatures(feature)) {
  172. childObj.children = getChildrenFeatures(Container(feature));
  173. } else {
  174. associateWithMapObject(childObj, feature);
  175. }
  176. childrenFeatures.push(childObj);
  177. }
  178. return childrenFeatures;
  179. }
  180. public function canContainFeatures(feature:Feature):Boolean {
  181. return (feature is Container);
  182. }
  183. public function findStyle(feature:Feature):void {
  184. if (feature.style) {
  185. kmlStyles[feature.style.id] = feature.style;
  186. }
  187. }
  188. public function getAlternateName(feature:Feature):String {
  189. if (feature is Folder) {
  190. return "Unnamed Folder";
  191. } else if (feature is Document) {
  192. return "Unnamed Document";
  193. } else if (feature is Placemark) {
  194. var placemark:Placemark = Placemark(feature);
  195. if (placemark.geometry != null) {
  196. if (placemark.geometry is KmlPoint) {
  197. return "Unnamed Point";
  198. } else if (placemark.geometry is LineString) {
  199. return "Unnamed Linestring";
  200. } else if (placemark.geometry is LinearRing) {
  201. return "Unnamed LinearRing";
  202. } else if (placemark.geometry is KmlPolygon) {
  203. return "Unnamed Polygon";
  204. }
  205. }
  206. return "Unnamed Placemark";
  207. } else if (feature is com.google.maps.extras.xmlparsers.kml.GroundOverlay::KmlGroundOverlay) {
  208. return "Unnamed GroundOverlay";
  209. }
  210. return "Unnamed Feature";
  211. }
  212. ]]>
  213. </mx:Script>
  214. </mx:Application>