<?xml version="1.0" encoding="utf-8"?>
<!--
    Copyright (c) 2008 Yahoo! Inc.  All rights reserved.  
    The copyrights embodied in the content of this file are licensed under the BSD (revised) open source license
-->
<mx:WindowedApplication 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    creationComplete="handleCreationComplete();" viewSourceURL="srcview/index.html">
            
    <mx:Panel id="mapPanel" title="Yahoo! Maps - AIR Demo" top="5" left="5" bottom="5" right="5">
        <mx:UIComponent id="mapContainer" width="100%" height="100%"/>
    </mx:Panel>
    
    <mx:Script>  
        <![CDATA[
            import mx.controls.Alert;
            
            import mx.events.ResizeEvent; 
            
            // import maps classes.
            import com.yahoo.maps.api.YahooMap; 
            import com.yahoo.maps.api.YahooMapEvent; 
            import com.yahoo.maps.api.core.location.LatLon;
            import com.yahoo.maps.api.markers.SimpleMarker;
            import com.yahoo.maps.webservices.geocoder.GeocoderResult; 
            import com.yahoo.maps.webservices.geocoder.GeocoderResultSet;
            import com.yahoo.maps.webservices.geocoder.events.GeocoderEvent; 

            private var _yahooMap:YahooMap;
            private var _locationList:Array;
             
            private function handleCreationComplete():void  
            { 
                getLocationList();
            }
            
            private function getLocationList():void
            {
                // copy the 'locations.txt' file located in the src directory to your desktop 
                
                var fileName:String = "locations.txt";
                var file:File = File.desktopDirectory.resolvePath(fileName);
                
                if(!file.exists) 
                {
                    mapPanel.visible = false;
                    Alert.show(file.nativePath + " does not exist", "File not found", 4, this, handleErrorAlertClose);
                    return;
                }
                
                var fileStream:FileStream = new FileStream();
                fileStream.open(file, FileMode.READ);
                
                var fileContents:String = fileStream.readMultiByte(file.size, File.systemCharset);
                fileStream.close();
                
                var latlonStrs:Array = fileContents.split('\n');
                
                _locationList = new Array();
                
                for each(var llstr:String in latlonStrs)
                {
                    var ll:LatLon = new LatLon(llstr.split(',')[0], llstr.split(',')[1]);
                    _locationList.push(ll);
                }
                
                addMap();
            }
            
            private function addMap():void
            {
                // Get a AppId from the Yahoo! Developer Network @ http://developer.yahoo.com/wsregapp/
                var appid:String = null;
                
                if(!appid || appid == "") 
                {
                    mapPanel.visible = false;
                    Alert.show("No AppID provided","Map Error", 4, this, handleErrorAlertClose);
                    return;
                }
                
                _yahooMap = new YahooMap(); 
                _yahooMap.addEventListener(YahooMapEvent.MAP_INITIALIZE, handleMapInitialize);
                _yahooMap.init(appid,mapContainer.width,mapContainer.height);
                 
                mapContainer.addChild(_yahooMap); 
                mapContainer.addEventListener(ResizeEvent.RESIZE, handleContainerResize); 
                 
                _yahooMap.addPanControl(); 
                _yahooMap.addZoomWidget(); 
                _yahooMap.addTypeWidget();  
            }
            
            private function handleMapInitialize(event:YahooMapEvent):void  
            {
                // set map zoom to default
                _yahooMap.zoomLevel = _yahooMap.config.defaultZoom;
                
                // set the initial location of the map to the first location pulled from the file.
                _yahooMap.centerLatLon = _locationList[0] as LatLon;
                
                // loop over each LatLon and add a marker to the map on that location.
                for each(var latlon:LatLon in _locationList)
                {
                    var marker:SimpleMarker = new SimpleMarker();
                    marker.latlon = latlon;
                    _yahooMap.markerManager.addMarker(marker);
                }
                
                // set the map to the bounds of the active markers.
                _yahooMap.setMapBounds( _yahooMap.markerManager.getMarkerBounds() );
            } 
            
            private function handleContainerResize(event:ResizeEvent):void 
            {
                // set the size of the map based on its containers size.
                _yahooMap.setSize(mapContainer.width,mapContainer.height); 
            }
            
            private function handleErrorAlertClose(event:Event):void
            {
                this.close();
            }
        ]]>  
    </mx:Script>
</mx:WindowedApplication>