<?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:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute" 
    creationComplete="handleCreationComplete();" 
    viewSourceURL="srcview/index.html">
      
    <mx:Panel title="Yahoo! Maps - Internet Location Demo" top="5" left="5" bottom="5" right="5" resizeEffect="Resize">
        <mx:UIComponent id="mapContainer" width="100%" height="100%"/>
    </mx:Panel>
    
    <mx:Script>  
        <![CDATA[
            
            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.Address;
            import com.yahoo.maps.webservices.geocoder.Geocoder;
            import com.yahoo.maps.webservices.geocoder.GeocoderResult; 
            import com.yahoo.maps.webservices.geocoder.GeocoderResultSet;
            import com.yahoo.maps.webservices.geocoder.events.GeocoderEvent; 

            import com.yahoo.maps.examples.SimpleCustomMarker;
            
            private var _yahooMap:YahooMap; 
            private var _address:Address; 
            private var _geocoder:Geocoder;
            private var _appId:String;
            
            private static const WHERE_NS:Namespace = new Namespace("http://where.yahooapis.com/v1/schema.rng");
             
            private function handleCreationComplete():void  
            { 
                // this examples uses an application id passed into the app via FlashVars.
                // Get your own from the Yahoo! Developer Network @ http://developer.yahoo.com/wsregapp/
                _appId = Application.application.parameters.appid;
                
                _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  
            { 
                // create a new Geocoder object to translate a WOEID to a LatLon.
                _geocoder = new Geocoder();
                _geocoder.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleWOEGeocodeSuccess);
                
                _address = new Address("seattle,wa"); 
                _address.addEventListener(GeocoderEvent.GEOCODER_SUCCESS, handleAddrGeocodeSuccess);
                _address.geocode(); 
            } 
             
            private function handleAddrGeocodeSuccess(event:GeocoderEvent):void  
            { 
                // retrieve the first result returned by the geocoder. 
                var result:GeocoderResult = _address.geocoderResultSet.firstResult; 
                
                // then we'll get the zoom level and center latlon to position the map on. 
                _yahooMap.zoomLevel = result.zoomLevel; 
                _yahooMap.centerLatLon = result.latlon; 
                
                getPlaceNeighbors(result.woeid, 0, 25);
            } 
             
            private function getPlaceNeighbors(woeid:uint, start:int=0, count:int=20):void
            {
                var url:String = "http://where.yahooapis.com/v1/place/"+woeid+"/neighbors;start="+start+";count="+count+"?format=xml&appid="+_appId;
                
                var req:URLRequest = new URLRequest();
                req.url = url;
                
                var urlLoader:URLLoader = new URLLoader();
                urlLoader.addEventListener(Event.COMPLETE, handleURLLoadComplete);
                urlLoader.load(req);
            }
             
            private function handleURLLoadComplete(event:Event):void
            {
                var target:URLLoader = (event.target as URLLoader);
                var xml:XML = XML(target.data);
                
                var places:XMLList = xml.WHERE_NS::place;
                
                // loop over each place in the XML doc.
                for each(var place:XML in places)
                {
                    // we must geocode the woeid in order to get the location (latitude and longitude) to place the marker at.
                    var woeid:uint = parseInt(place.WHERE_NS::woeid);
                    _geocoder.geocodeWOEId(woeid);
                }
            }
            
            private function handleWOEGeocodeSuccess(event:GeocoderEvent):void
            {
                var resultSet:GeocoderResultSet = (event.data as GeocoderResultSet);
                var firstResult:GeocoderResult = resultSet.firstResult;
                
                var marker:SimpleCustomMarker = new SimpleCustomMarker();
                marker.latlon = firstResult.latlon;
                _yahooMap.markerManager.addMarker(marker);
            }
            
            private function handleContainerResize(event:ResizeEvent):void 
            {
                // set the size of the map based on its containers size.
                _yahooMap.setSize(mapContainer.width,mapContainer.height); 
            }
        ]]>  
    </mx:Script>  
</mx:Application>