nice zoom buttons and a loading message
[potlatch2.git] / net / systemeD / halcyon / connection / XMLConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.Event;
4     import flash.net.URLLoader;
5     import flash.net.URLRequest;
6
7         import flash.system.Security;
8         import flash.net.*;
9
10
11         public class XMLConnection extends Connection {
12
13         public var readConnection:NetConnection;
14
15                 public function XMLConnection() {
16
17                         if (Connection.policyURL!='')
18                 Security.loadPolicyFile(Connection.policyURL);
19
20                         readConnection=new NetConnection();
21                         readConnection.objectEncoding = flash.net.ObjectEncoding.AMF0;
22                         readConnection.connect(Connection.apiBaseURL+"amf/read");
23                         
24                 }
25
26                 override public function getEnvironment(responder:Responder):void {
27                         readConnection.call("getpresets",responder,"en");
28                 }
29                 
30                 override public function loadBbox(left:Number,right:Number,
31                                                                 top:Number,bottom:Number):void {
32             var mapVars:URLVariables = new URLVariables();
33             mapVars.bbox= left+","+bottom+","+right+","+top;
34
35             var mapRequest:URLRequest = new URLRequest(Connection.apiBaseURL+"map");
36             mapRequest.data = mapVars;
37
38             var mapLoader:URLLoader = new URLLoader();
39             mapLoader.addEventListener(Event.COMPLETE, loadedMap);
40             mapLoader.load(mapRequest);
41             dispatchEvent(new Event(LOAD_STARTED));
42                 }
43
44         private function parseTags(tagElements:XMLList):Object {
45             var tags:Object = {};
46             for each (var tagEl:XML in tagElements)
47                 tags[tagEl.@k] = tagEl.@v;
48             return tags;
49         }
50
51         private function loadedMap(event:Event):void {
52             dispatchEvent(new Event(LOAD_COMPLETED));
53
54             var map:XML = new XML(URLLoader(event.target).data);
55             var id:Number;
56             var version:uint;
57             var tags:Object;
58
59             for each(var nodeData:XML in map.node) {
60                 id = Number(nodeData.@id);
61                 version = uint(nodeData.@version);
62
63                 var node:Node = getNode(id);
64                 if ( node == null ) {
65                     var lat:Number = Number(nodeData.@lat);
66                     var lon:Number = Number(nodeData.@lon);
67                     tags = parseTags(nodeData.tag);
68                     setNode(new Node(id, version, tags, lat, lon));
69                 }
70             }
71
72             for each(var data:XML in map.way) {
73                 id = Number(data.@id);
74                 version = uint(data.@version);
75
76                 var way:Way = getWay(id);
77                 if ( way == null ) {
78                     var nodes:Array = [];
79                     for each(var nd:XML in data.nd)
80                         nodes.push(getNode(Number(nd.@ref)));
81                     tags = parseTags(data.tag);
82                     setWay(new Way(id, version, tags, nodes));
83                 }
84             }
85         }
86
87         }
88 }