<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  width="500" height="550"
  layout="vertical" verticalGap="0" 
  paddingBottom="0" paddingTop="0"
  paddingLeft="0" paddingRight="0"
  creationComplete="initApp()" viewSourceURL="srcview/index.html">
  <mx:Script>
  <![CDATA[
    import com.yahoo.maps.api.markers.SearchMarker;
    import com.yahoo.maps.webservices.local.LocalSearchItem;
    import com.yahoo.maps.webservices.local.LocalSearchResults;
    import com.yahoo.maps.webservices.local.events.LocalSearchEvent;
    import com.yahoo.maps.webservices.local.LocalSearch;
    import com.yahoo.maps.api.core.location.LatLon;
    import com.yahoo.maps.api.YahooMapEvent;
    import com.yahoo.maps.api.YahooMap;
    
    private var ymap:YahooMap;
    
    private var localSearch:LocalSearch;
    
    private function initApp():void
    {
      this.ymap = new YahooMap("appid", this.width, this.height);
      this.ymap.addEventListener(
        YahooMapEvent.MAP_INITIALIZE, initializeMap);
        
      this.ymap.addPanControl();
      this.ymap.addZoomWidget();
      this.ymap.addTypeWidget();
      
      this.mapContainer.addChild(this.ymap);   
    }
    
    private function initializeMap(event:YahooMapEvent):void
    {
      this.ymap.zoomLevel = 4;
      this.ymap.centerLatLon = new LatLon(40.714, -74.006);
      
      this.doLocalSearch();
    }
    
    private function checkEnterKey(event:KeyboardEvent):void
    {
      if(event.keyCode == Keyboard.ENTER)
      {
        doLocalSearch();
      }
    }
    
    private function doLocalSearch():void
    {
      this.localSearch = new LocalSearch();
      this.localSearch.addEventListener(
        LocalSearchEvent.SEARCH_SUCCESS, handleSearchResults);
      this.localSearch.searchLocal
        (
          this.searchTerm.text,
          this.ymap.zoomLevel,
          this.ymap.centerLatLon
        );
    }
    
    private function handleSearchResults(event:LocalSearchEvent)
      :void
    {
      this.ymap.markerManager.removeAllMarkers();
      
      var srcResults:LocalSearchResults 
        = event.data as LocalSearchResults;
      var results:Array = srcResults.results;
      
      for(var i:int = 0; i < results.length; i++)
      {
        var item:LocalSearchItem = results[i];
        var marker:SearchMarker = new SearchMarker(item);
        this.ymap.markerManager.addMarker(marker);
      }
    }
  ]]>
  </mx:Script>
  <mx:HBox width="100%" horizontalAlign="left" 
    paddingBottom="2" paddingTop="2">
    <mx:Label text="Local Search" />
    <mx:TextInput id="searchTerm" width="200" text="italian" 
      keyDown="{checkEnterKey(event)}" />
    <mx:Button label="Search" click="{doLocalSearch()}" />  
  </mx:HBox>
  <mx:UIComponent id="mapContainer" width="100%" height="100%"/>
</mx:Application>