Different bug symbols for fixed/invalid bugs
[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         private static const status:Array = ["", "open", "fixed", "invalid"];
20
21         public function BugLoader(map:Map, url:String, bugApiKey:String):void {
22             this.map = map;
23             this.bugBaseURL = url;
24             this.bugApiKey = bugApiKey;
25             var policyFile:String = bugBaseURL+"crossdomain.xml";
26             Security.loadPolicyFile(policyFile);
27         }
28
29         public function load():void {
30             var loader:URLLoader = new URLLoader();
31             loader.load(new URLRequest(bugBaseURL+"getBugs?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+bugApiKey));
32             loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, balls);
33             loader.addEventListener(Event.COMPLETE, parseJSON);
34         }
35
36         public function balls(event:SecurityErrorEvent):void {
37             trace(event);
38         }
39
40         private function parseJSON(event:Event):void {
41             var result:String = String(event.target.data);
42             var featureCollection:Object = JSON.decode(result);
43
44             for each (var feature:Object in featureCollection.features) {
45               // geoJSON spec is x,y,z i.e. lon, lat, ele
46               var lon:Number = feature.geometry.coordinates[0];
47               var lat:Number = feature.geometry.coordinates[1];
48               var tags:Object = {};
49               tags["name"] = String(feature.properties.description).substr(0,10)+'...';
50               tags["description"] = feature.properties.description;
51               tags["bug_id"] = feature.id;
52               tags["nickname"] = feature.properties.nickname;
53               tags["type"] = feature.properties.type;
54               tags["date_created"] = feature.properties.date_created;
55               tags["date_updated"] = feature.properties.date_updated;
56               tags["source"] = feature.properties.source;
57               tags["status"] = status[int(feature.properties.status)];
58               var marker:Marker = layer.createMarker(tags, lat, lon);
59             }
60             layer.paint.updateEntityUIs(layer.getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
61         }
62
63         private function get layer():VectorLayer {
64             if (!_layer) {
65                 var n:String='Bugs';
66                 _layer=new BugLayer(n,map,STYLESHEET,bugBaseURL,bugApiKey);
67                 map.addVectorLayer(_layer);
68             }
69             return _layer;
70         }
71     }
72 }