/examples/KMLParser/src/KMLParser.mxml

http://gmaps-utility-library-flash.googlecode.com/ · Macromedia eXtensible Markup Language · 211 lines · 194 code · 17 blank · 0 comment · 0 complexity · bf871693732789fad1019b47ae8c96e2 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.Marker;
  31. import com.google.maps.MapEvent;
  32. import com.google.maps.Map;
  33. import com.google.maps.MapType;
  34. import com.google.maps.LatLng;
  35. import com.google.maps.LatLngBounds;
  36. import com.google.maps.overlays.GroundOverlay;
  37. import com.google.maps.overlays.GroundOverlayOptions;
  38. import com.google.maps.extras.xmlparsers.kml.*;
  39. import mx.controls.Alert;
  40. [Bindable] private var kmlObj:Object = new Object();
  41. public var SAMPLE_FILES:Array = ["sample.xml", "debug.xml", "multigeometry.xml", "polygon.xml"];
  42. public function onMapReady(event:MapEvent):void {
  43. map.setCenter(new LatLng(37.422289,-122.0822035), 14, MapType.NORMAL_MAP_TYPE);
  44. map.addControl(new ZoomControl());
  45. loadFile(null);
  46. }
  47. public function loadFile(event:Event):void {
  48. var file:String = kmlFiles.selectedLabel;
  49. var loader:URLLoader = new URLLoader();
  50. loader.addEventListener(Event.COMPLETE, loadXML);
  51. loader.load(new URLRequest(file));
  52. }
  53. public function loadXML(event:Event):void {
  54. var kml:Kml22 = new Kml22(event.target.data);
  55. var rootFeature:Feature = kml.feature;
  56. // @todo: This is redundant with code in getChildrenFeatures, should combine
  57. kmlObj = new Object();
  58. kmlObj.name = rootFeature.name;
  59. kmlObj.mapObjs = new Array();
  60. kmlObj.bounds = new LatLngBounds();
  61. if (canContainFeatures(rootFeature)) {
  62. kmlObj.children = getChildrenFeatures(Container(rootFeature));
  63. } else {
  64. associateWithMapObject(kmlObj, rootFeature);
  65. }
  66. map.setCenter(kmlObj.bounds.getCenter(), map.getBoundsZoomLevel(kmlObj.bounds));
  67. }
  68. private function changeEvt(event:Event):void {
  69. var currentObj:Object = event.currentTarget.selectedItem;
  70. if (currentObj.bounds) { // Otherwise it's a folder
  71. map.setCenter(currentObj.bounds.getCenter(), map.getBoundsZoomLevel(currentObj.bounds));
  72. }
  73. }
  74. public function associateWithMapObject(obj:Object, feature:Feature):void {
  75. // at this point it can either be a placemark or a groundoverlay
  76. if (feature is Placemark) {
  77. var placemark:Placemark = Placemark(feature);
  78. if (placemark.geometry != null) {
  79. associateGeometryWithMapObject(obj, placemark.geometry);
  80. }
  81. } else if (feature is KmlGroundOverlay) {
  82. var groundOverlay:KmlGroundOverlay = KmlGroundOverlay(feature);
  83. var latLngBounds:LatLngBounds = new LatLngBounds(new LatLng(groundOverlay.latLonBox.south,groundOverlay.latLonBox.west), new LatLng(groundOverlay.latLonBox.north,groundOverlay.latLonBox.east));
  84. updateBounds(obj, latLngBounds);
  85. var testLoader:Loader = new Loader();
  86. var urlRequest:URLRequest = new URLRequest(groundOverlay.icon.href);
  87. testLoader.contentLoaderInfo.addEventListener(
  88. Event.COMPLETE,
  89. function(e:Event):void {
  90. obj.mapObject = new com.google.maps.overlays.GroundOverlay::GroundOverlay(testLoader, latLngBounds);
  91. map.addOverlay(obj.mapObject);
  92. });
  93. testLoader.load(urlRequest);
  94. }
  95. }
  96. public function associateGeometryWithMapObject(obj:Object, geometry:Geometry):void {
  97. if (geometry is KmlPoint) {
  98. var point:KmlPoint = KmlPoint(geometry);
  99. var latlng:LatLng = new LatLng(point.coordinates.coordsList[0].lat, point.coordinates.coordsList[0].lon);
  100. obj.mapObjs.push(new Marker(latlng));
  101. updateBounds(obj, new LatLngBounds(latlng, latlng));
  102. map.addOverlay(obj.mapObjs[obj.mapObjs.length -1]);
  103. } else if (geometry is LineString) {
  104. var lineString:LineString = LineString(geometry);
  105. var polyline:Polyline = new Polyline(getCoordinatesLatLngs(lineString.coordinates));
  106. obj.mapObjs.push(polyline);
  107. updateBounds(obj, polyline.getLatLngBounds());
  108. obj.center = polyline.getLatLngBounds().getCenter();
  109. obj.bounds = polyline.getLatLngBounds();
  110. map.addOverlay(polyline);
  111. } else if (geometry is LinearRing) {
  112. var linearRing:LinearRing = LinearRing(geometry);
  113. var polyline:Polyline = new Polyline(getCoordinatesLatLngs(linearRing.coordinates));
  114. obj.mapObjs.push(polyline);
  115. updateBounds(obj, polyline.getLatLngBounds());
  116. map.addOverlay(polyline);
  117. } else if (geometry is KmlPolygon) {
  118. var kmlPolygon:KmlPolygon = KmlPolygon(geometry);
  119. var polygon:Polygon = new Polygon(getCoordinatesLatLngs(kmlPolygon.outerBoundaryIs.linearRing.coordinates));
  120. obj.mapObjs.push(polygon);
  121. updateBounds(obj, polygon.getLatLngBounds());
  122. map.addOverlay(polygon);
  123. } else if (geometry is MultiGeometry) {
  124. var multiGeometry:MultiGeometry = MultiGeometry(geometry);
  125. for (var i:uint = 0; i < multiGeometry.geometries.length; i++) {
  126. associateGeometryWithMapObject(obj, multiGeometry.geometries[i]);
  127. }
  128. }
  129. }
  130. public function updateBounds(obj:Object, bounds:LatLngBounds):void {
  131. if (obj.bounds) {
  132. obj.bounds.union(bounds);
  133. } else {
  134. obj.bounds = bounds;
  135. }
  136. kmlObj.bounds.union(bounds);
  137. }
  138. public function getCoordinatesLatLngs(coordinates:Coordinates):Array {
  139. var latlngs:Array = new Array();
  140. for (var i:Number = 0; i < coordinates.coordsList.length; i++) {
  141. var coordinate:Object = coordinates.coordsList[i];
  142. latlngs.push(new LatLng(Number(coordinate.lat), Number(coordinate.lon)));
  143. }
  144. return latlngs;
  145. }
  146. public function getChildrenFeatures(container:Container):Array {
  147. var childrenFeatures:Array = new Array();
  148. for (var i:Number = 0; i < container.features.length; i++) {
  149. var feature:Feature = container.features[i];
  150. var childObj:Object = new Object();
  151. childObj.mapObjs = new Array();
  152. childObj.name = feature.name;
  153. if (childObj.name == null) {
  154. childObj.name = getAlternateName(feature);
  155. }
  156. if (canContainFeatures(feature)) {
  157. childObj.children = getChildrenFeatures(Container(feature));
  158. } else {
  159. associateWithMapObject(childObj, feature);
  160. }
  161. childrenFeatures.push(childObj);
  162. }
  163. return childrenFeatures;
  164. }
  165. public function canContainFeatures(feature:Feature):Boolean {
  166. return (feature is Container);
  167. }
  168. public function getAlternateName(feature:Feature):String {
  169. if (feature is Folder) {
  170. return "Unnamed Folder";
  171. } else if (feature is Document) {
  172. return "Unnamed Document";
  173. } else if (feature is Placemark) {
  174. var placemark:Placemark = Placemark(feature);
  175. if (placemark.geometry != null) {
  176. if (placemark.geometry is KmlPoint) {
  177. return "Unnamed Point";
  178. } else if (placemark.geometry is LineString) {
  179. return "Unnamed Linestring";
  180. } else if (placemark.geometry is LinearRing) {
  181. return "Unnamed LinearRing";
  182. } else if (placemark.geometry is KmlPolygon) {
  183. return "Unnamed Polygon";
  184. }
  185. }
  186. return "Unnamed Placemark";
  187. } else if (feature is com.google.maps.extras.xmlparsers.kml.GroundOverlay::KmlGroundOverlay) {
  188. return "Unnamed GroundOverlay";
  189. }
  190. return "Unnamed Feature";
  191. }
  192. ]]>
  193. </mx:Script>
  194. </mx:Application>