Create the concept of Marker entities, and use them for 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.Node;
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         public function parseJSON(event:Event):void {
31             trace("parseJSON");
32             var result:String = String(event.target.data);
33             var featureCollection:Object = JSON.decode(result);
34             trace(featureCollection);
35             trace(featureCollection.type);
36             trace(featureCollection.features[0].type);
37             trace(featureCollection.features.length);
38             for each (var feature:Object in featureCollection.features) {
39               // geoJSON spec is x,y,z i.e. lon, lat, ele
40               var lon:Number = feature.geometry.coordinates[0];
41               var lat:Number = feature.geometry.coordinates[1];
42               trace(lat, lon);
43               var node:Node = layer.createMarker({"name":feature.properties.description,"bug_id":feature.id}, lat, lon);
44               layer.registerPOI(node);
45             }
46             layer.paint.updateEntityUIs(layer.getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), false, false);
47             //var json:Array =
48         }
49
50         private function get layer():VectorLayer {
51             if (!_layer) {
52                 var n:String='Bugs';
53                 _layer=new VectorLayer(n,map,STYLESHEET);
54                 map.addVectorLayer(_layer);
55             }
56             return _layer;
57         }
58     }
59 }