PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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