/examples/StyledKMLParser/src/KMLParser.mxml
Macromedia eXtensible Markup Language | 233 lines | 214 code | 19 blank | 0 comment | 0 complexity | a6ed0a945b12fba099a19a8957246572 MD5 | raw file
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- Copyright 2008 Google Inc.
- Licensed under the Apache License, Version 2.0:
- http://www.apache.org/licenses/LICENSE-2.0
- -->
- <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">
- <mx:Panel title="Google Maps API for Flash Demo" width="100%" height="100%">
- <mx:VBox width="100%" height="100%">
- <mx:ComboBox
- id="kmlFiles"
- dataProvider = "{SAMPLE_FILES}"
- change="loadFile(event)"/>
- <mx:HDividedBox width="100%" height="100%">
- <mx:Tree id="kmlTree" width="200" height="100%" dataProvider="{kmlObj}" change="changeEvt(event);" labelField="name" showRoot="false"/>
- <maps:Map
- id="map"
- key="ABQIAAAA7QUChpcnvnmXxsjC7s1fCxQGj0PqsCtxKvarsoS-iqLdqZSKfxTd7Xf-2rEc_PC9o8IsJde80Wnj4g"
- mapevent_mapready="onMapReady(event)"
- width="100%" height="100%"/>
- </mx:HDividedBox>
- </mx:VBox>
- </mx:Panel>
- <mx:Script>
- <![CDATA[
- import flash.events.Event;
-
- import com.google.maps.controls.ZoomControl;
- import com.google.maps.overlays.Polyline;
- import com.google.maps.overlays.Polygon;
- import com.google.maps.overlays.PolygonOptions;
- import com.google.maps.overlays.Marker;
- import com.google.maps.MapEvent;
- import com.google.maps.Map;
- import com.google.maps.MapType;
- import com.google.maps.LatLng;
- import com.google.maps.LatLngBounds;
- import com.google.maps.overlays.GroundOverlay;
- import com.google.maps.overlays.GroundOverlayOptions;
- import com.google.maps.styles.*;
- import com.google.maps.extras.xmlparsers.kml.*;
- import mx.controls.Alert;
-
- [Bindable] private var kmlObj:Object = new Object();
- private var kmlStyles:Object = new Object();
-
- public var SAMPLE_FILES:Array = ["styledpoly.xml", "debug.xml", "multigeometry.xml", "polygon.xml"];
-
- public function onMapReady(event:MapEvent):void {
- map.setCenter(new LatLng(37.422289,-122.0822035), 14, MapType.NORMAL_MAP_TYPE);
- map.addControl(new ZoomControl());
- loadFile(null);
- }
-
- public function loadFile(event:Event):void {
- var file:String = kmlFiles.selectedLabel;
- var loader:URLLoader = new URLLoader();
- loader.addEventListener(Event.COMPLETE, loadXML);
- loader.load(new URLRequest(file));
- }
-
- public function loadXML(event:Event):void {
- var kml:Kml22 = new Kml22(event.target.data);
- var rootFeature:Feature = kml.feature;
- findStyle(rootFeature);
-
- // @todo: This is redundant with code in getChildrenFeatures, should combine
- kmlObj = new Object();
- kmlObj.name = rootFeature.name;
- kmlObj.mapObjs = new Array();
- kmlObj.bounds = new LatLngBounds();
-
- if (canContainFeatures(rootFeature)) {
- kmlObj.children = getChildrenFeatures(Container(rootFeature));
- } else {
- associateWithMapObject(kmlObj, rootFeature);
- }
- map.setCenter(kmlObj.bounds.getCenter(), map.getBoundsZoomLevel(kmlObj.bounds));
- }
-
- private function changeEvt(event:Event):void {
- var currentObj:Object = event.currentTarget.selectedItem;
- if (currentObj.bounds) { // Otherwise it's a folder
- map.setCenter(currentObj.bounds.getCenter(), map.getBoundsZoomLevel(currentObj.bounds));
- }
- }
-
-
- public function associateWithMapObject(obj:Object, feature:Feature):void {
- // at this point it can either be a placemark or a groundoverlay
- if (feature is Placemark) {
- var placemark:Placemark = Placemark(feature);
- if (placemark.geometry != null) {
- associateGeometryWithMapObject(obj, placemark.geometry, placemark.styleUrl);
- }
- } else if (feature is KmlGroundOverlay) {
- var groundOverlay:KmlGroundOverlay = KmlGroundOverlay(feature);
- var latLngBounds:LatLngBounds = new LatLngBounds(new LatLng(groundOverlay.latLonBox.south,groundOverlay.latLonBox.west), new LatLng(groundOverlay.latLonBox.north,groundOverlay.latLonBox.east));
- updateBounds(obj, latLngBounds);
- var testLoader:Loader = new Loader();
- var urlRequest:URLRequest = new URLRequest(groundOverlay.icon.href);
- testLoader.contentLoaderInfo.addEventListener(
- Event.COMPLETE,
- function(e:Event):void {
- obj.mapObject = new com.google.maps.overlays.GroundOverlay::GroundOverlay(testLoader, latLngBounds);
- map.addOverlay(obj.mapObject);
- });
- testLoader.load(urlRequest);
- }
- }
-
- public function associateGeometryWithMapObject(obj:Object, geometry:Geometry, styleUrl:String):void {
- if (geometry is KmlPoint) {
- var point:KmlPoint = KmlPoint(geometry);
- var latlng:LatLng = new LatLng(point.coordinates.coordsList[0].lat, point.coordinates.coordsList[0].lon);
- obj.mapObjs.push(new Marker(latlng));
- updateBounds(obj, new LatLngBounds(latlng, latlng));
- map.addOverlay(obj.mapObjs[obj.mapObjs.length -1]);
- } else if (geometry is LineString) {
- var lineString:LineString = LineString(geometry);
- var polyline:Polyline = new Polyline(getCoordinatesLatLngs(lineString.coordinates));
- obj.mapObjs.push(polyline);
- updateBounds(obj, polyline.getLatLngBounds());
- obj.center = polyline.getLatLngBounds().getCenter();
- obj.bounds = polyline.getLatLngBounds();
- map.addOverlay(polyline);
- } else if (geometry is LinearRing) {
- var linearRing:LinearRing = LinearRing(geometry);
- var polyline:Polyline = new Polyline(getCoordinatesLatLngs(linearRing.coordinates));
- obj.mapObjs.push(polyline);
- updateBounds(obj, polyline.getLatLngBounds());
- map.addOverlay(polyline);
- } else if (geometry is KmlPolygon) {
- var kmlPolygon:KmlPolygon = KmlPolygon(geometry);
- var options:PolygonOptions = new PolygonOptions();
- if (styleUrl) {
- styleUrl = styleUrl.replace("#", "");
- if (kmlStyles[styleUrl]) {
- var style:Style = kmlStyles[styleUrl];
- options.fillStyle = new FillStyle();
- options.fillStyle.color = style.polyStyle.color;
- options.strokeStyle = new StrokeStyle();
- options.strokeStyle.thickness = style.lineStyle.width;
- }
- }
- var polygon:Polygon = new Polygon(getCoordinatesLatLngs(kmlPolygon.outerBoundaryIs.linearRing.coordinates), options);
- obj.mapObjs.push(polygon);
- updateBounds(obj, polygon.getLatLngBounds());
- map.addOverlay(polygon);
- } else if (geometry is MultiGeometry) {
- var multiGeometry:MultiGeometry = MultiGeometry(geometry);
- for (var i:uint = 0; i < multiGeometry.geometries.length; i++) {
- associateGeometryWithMapObject(obj, multiGeometry.geometries[i], null);
- }
- }
- }
-
- public function updateBounds(obj:Object, bounds:LatLngBounds):void {
- if (obj.bounds) {
- obj.bounds.union(bounds);
- } else {
- obj.bounds = bounds;
- }
- kmlObj.bounds.union(bounds);
- }
-
- public function getCoordinatesLatLngs(coordinates:Coordinates):Array {
- var latlngs:Array = new Array();
- for (var i:Number = 0; i < coordinates.coordsList.length; i++) {
- var coordinate:Object = coordinates.coordsList[i];
- latlngs.push(new LatLng(Number(coordinate.lat), Number(coordinate.lon)));
- }
- return latlngs;
- }
-
- public function getChildrenFeatures(container:Container):Array {
- var childrenFeatures:Array = new Array();
- for (var i:Number = 0; i < container.features.length; i++) {
- var feature:Feature = container.features[i];
- var childObj:Object = new Object();
- childObj.mapObjs = new Array();
- childObj.name = feature.name;
- if (childObj.name == null) {
- childObj.name = getAlternateName(feature);
- }
- if (canContainFeatures(feature)) {
- childObj.children = getChildrenFeatures(Container(feature));
- } else {
- associateWithMapObject(childObj, feature);
- }
- childrenFeatures.push(childObj);
- }
- return childrenFeatures;
- }
-
- public function canContainFeatures(feature:Feature):Boolean {
- return (feature is Container);
- }
-
- public function findStyle(feature:Feature):void {
- if (feature.style) {
- kmlStyles[feature.style.id] = feature.style;
- }
- }
-
- public function getAlternateName(feature:Feature):String {
- if (feature is Folder) {
- return "Unnamed Folder";
- } else if (feature is Document) {
- return "Unnamed Document";
- } else if (feature is Placemark) {
- var placemark:Placemark = Placemark(feature);
- if (placemark.geometry != null) {
- if (placemark.geometry is KmlPoint) {
- return "Unnamed Point";
- } else if (placemark.geometry is LineString) {
- return "Unnamed Linestring";
- } else if (placemark.geometry is LinearRing) {
- return "Unnamed LinearRing";
- } else if (placemark.geometry is KmlPolygon) {
- return "Unnamed Polygon";
- }
- }
- return "Unnamed Placemark";
-
- } else if (feature is com.google.maps.extras.xmlparsers.kml.GroundOverlay::KmlGroundOverlay) {
- return "Unnamed GroundOverlay";
- }
- return "Unnamed Feature";
- }
- ]]>
- </mx:Script>
- </mx:Application>