adding nominatim to P2
[potlatch2.git] / net / systemeD / potlatch2 / find / FindDialog.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         layout="vertical" showCloseButton="true"
5         horizontalAlign="center" title="Find a place in OpenStreetMap (powered by Nominatim Search)"
6         width="458" height="350" verticalGap="0">
7         <mx:Script><![CDATA[
8                 import mx.core.Application;
9                 import mx.events.CloseEvent;
10                 import mx.events.FlexEvent;
11                 import mx.managers.PopUpManager;
12                 import mx.rpc.events.ResultEvent;
13                 import mx.rpc.http.HTTPService;
14                 import mx.controls.Alert;
15                 
16                 import net.systemeD.halcyon.Globals;
17                 
18                 private var nominatim:HTTPService = new HTTPService;
19                 private var nominatimURL:String = "http://open.mapquestapi.com/nominatim/v1/search?";
20                 private var nomOptions:Object = { addressdetails:"1", limit:"10", format:"xml" };
21
22                 [Bindable]
23         private var searchResults:XMLList;
24                                 
25                 
26                 public function init():void {
27                         PopUpManager.addPopUp(this, Application(Application.application), true);
28                         PopUpManager.centerPopUp(this);
29                         this.addEventListener(CloseEvent.CLOSE, findDialog_close);
30                         
31                         var obj:SharedObject = SharedObject.getLocal("user_state");
32         
33                 }
34                 
35                 private function findDialog_close(evt:CloseEvent):void {
36                         PopUpManager.removePopUp(this);
37                 }
38
39                 private function goFetch(q:String):void {
40
41                         var searchCall:String = nominatimURL;
42                         for (var item:String in nomOptions) {
43                                 searchCall += item + "="; 
44                                 searchCall += nomOptions[item] + "&";
45                         }
46                         searchCall += "q=" + q;
47                         this.nominatim.url = searchCall
48                         this.nominatim.addEventListener(ResultEvent.RESULT, goneFetched);
49                         this.nominatim.resultFormat = 'e4x';
50
51                         this.nominatim.send();
52                         
53                 }
54                 
55                 private function goneFetched(e:ResultEvent):void {
56                         var resultXML:XML = e.result as XML;
57             var resultList:XMLList = resultXML..place as XMLList;
58                         //Alert.show(resultXML);
59             searchResults = e.result..place as XMLList;
60             var sr2:XMLList = e.result.searchresults as XMLList;
61             var sr3:XMLList = e.result.searchresults.place as XMLList;
62             
63             //Alert.show(searchResults);
64
65             Alert.show("SR1 = " + searchResults.length);
66             Alert.show("SR2 = " + sr2.length());
67             Alert.show("SR3 = " + sr3.length());
68             Alert.show("SR4 = " + resultList.length());            
69                 }
70
71                 protected function btnFind_clickHandler(event:MouseEvent):void
72                 {
73                         goFetch(txtFind.text);
74                 }
75
76
77                 protected function txtFind_enterHandler(event:FlexEvent):void
78                 {
79                         goFetch(txtFind.text);
80                 }
81
82         ]]>
83         </mx:Script>
84         
85         <mx:ControlBar>
86                 <mx:Spacer width="100%"/>
87                 <mx:Button label="close" click="PopUpManager.removePopUp(this);" styleName="titleWindowButton" />
88         </mx:ControlBar>
89
90         <mx:HBox>
91                 <mx:TextInput id="txtFind" width="250" enter="txtFind_enterHandler(event)"/>
92                 <mx:Button label="Find" id="btnFind" click="btnFind_clickHandler(event)"/>              
93         </mx:HBox>
94         
95         <mx:Spacer height="20"/>
96         
97         <mx:DataGrid id="dgResults" dataProvider="{searchResults}">
98                 <mx:columns>
99                         <mx:DataGridColumn headerText="Name" dataField="@display_name"/>
100                 </mx:columns>
101         </mx:DataGrid>
102
103
104 </mx:TitleWindow>