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