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