Cleanup bugloader traces, and add more tags
[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 flash.net.*;
7     import flash.events.*;
8     import com.adobe.serialization.json.JSON;
9
10     public class BugLoader {
11
12         private var map:Map;
13         private var bugBaseURL:String;
14         private var bugApiKey:String;
15         private var _layer:VectorLayer;
16         private static const STYLESHEET:String="bugs.css";
17
18         public function BugLoader(map:Map, url:String, bugApiKey:String):void {
19             this.map = map;
20             this.bugBaseURL = url;
21             this.bugApiKey = bugApiKey;
22         }
23
24         public function load():void {
25             var loader:URLLoader = new URLLoader();
26             loader.load(new URLRequest(bugBaseURL+"getBugs.json?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t+"&key="+bugApiKey));
27             loader.addEventListener(Event.COMPLETE, parseJSON);
28         }
29
30         private function parseJSON(event:Event):void {
31             var result:String = String(event.target.data);
32             var featureCollection:Object = JSON.decode(result);
33
34             for each (var feature:Object in featureCollection.features) {
35               // geoJSON spec is x,y,z i.e. lon, lat, ele
36               var lon:Number = feature.geometry.coordinates[0];
37               var lat:Number = feature.geometry.coordinates[1];
38               var tags:Object = {};
39               tags["name"] = String(feature.properties.description).substr(0,10)+'...';
40               tags["description"] = feature.properties.description;
41               tags["bug_id"] = feature.id;
42               tags["nickname"] = feature.properties.nickname;
43               tags["type"] = feature.properties.type;
44               tags["date_created"] = feature.properties.date_created;
45               tags["date_updated"] = feature.properties.date_updated;
46               tags["source"] = feature.properties.source;
47               var marker:Marker = layer.createMarker(tags, lat, lon);
48             }
49             layer.paint.updateEntityUIs(layer.getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
50         }
51
52         private function get layer():VectorLayer {
53             if (!_layer) {
54                 var n:String='Bugs';
55                 _layer=new VectorLayer(n,map,STYLESHEET);
56                 map.addVectorLayer(_layer);
57             }
58             return _layer;
59         }
60     }
61 }