Make the bug layer work with remote server - use POST, crossdomain.xml
[potlatch2.git] / net / systemeD / potlatch2 / utils / BugLoader.as
1 package net.systemeD.potlatch2.utils {
2
3     import net.systemeD.halcyon.Map;
4     import net.systemeD.halcyon.VectorLayer;
5     import net.systemeD.halcyon.connection.Marker;
6     import net.systemeD.potlatch2.BugLayer;
7     import flash.net.*;
8     import flash.events.*;
9     import com.adobe.serialization.json.JSON;
10     import flash.system.Security;
11
12     public class BugLoader {
13
14         private var map:Map;
15         private var bugBaseURL:String;
16         private var bugApiKey:String;
17         private var _layer:VectorLayer;
18         private static const STYLESHEET:String="bugs.css";
19
20         public function BugLoader(map:Map, url:String, bugApiKey:String):void {
21             this.map = map;
22             this.bugBaseURL = url;
23             this.bugApiKey = bugApiKey;
24             var policyFile:String = bugBaseURL+"crossdomain.xml";
25             Security.loadPolicyFile(policyFile);
26         }
27
28         public function load():void {
29             var loader:URLLoader = new URLLoader();
30             loader.load(new URLRequest(bugBaseURL+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+bugApiKey));
31             loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, balls);
32             loader.addEventListener(Event.COMPLETE, parseJSON);
33         }
34
35         public function balls(event:SecurityErrorEvent):void {
36             trace(event);
37         }
38
39         private function parseJSON(event:Event):void {
40             var result:String = String(event.target.data);
41             var featureCollection:Object = JSON.decode(result);
42
43             for each (var feature:Object in featureCollection.features) {
44               // geoJSON spec is x,y,z i.e. lon, lat, ele
45               var lon:Number = feature.geometry.coordinates[0];
46               var lat:Number = feature.geometry.coordinates[1];
47               var tags:Object = {};
48               tags["name"] = String(feature.properties.description).substr(0,10)+'...';
49               tags["description"] = feature.properties.description;
50               tags["bug_id"] = feature.id;
51               tags["nickname"] = feature.properties.nickname;
52               tags["type"] = feature.properties.type;
53               tags["date_created"] = feature.properties.date_created;
54               tags["date_updated"] = feature.properties.date_updated;
55               tags["source"] = feature.properties.source;
56               var marker:Marker = layer.createMarker(tags, lat, lon);
57             }
58             layer.paint.updateEntityUIs(layer.getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
59         }
60
61         private function get layer():VectorLayer {
62             if (!_layer) {
63                 var n:String='Bugs';
64                 _layer=new BugLayer(n,map,STYLESHEET,bugBaseURL,bugApiKey);
65                 map.addVectorLayer(_layer);
66             }
67             return _layer;
68         }
69     }
70 }