Load policy files only when the layer is created - often the loader can be created...
[potlatch2.git] / net / systemeD / potlatch2 / utils / BikeShopLoader.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     /**
13     * The BikeShopLoader loads data regarding missing bike shops in the UK from the "bike-shop-locator" project.
14     * It was a quick hack undertaken during the WhereCampUK meeting in Nottingham in November 2010. It served partly
15     * as a demonstration of improving QA feedback loops within the OSM universe, but mainly as an indication
16     * that we need to rethink how these things are handled in P2. The amount of copy/paste coding going on is too
17     * high, and we should be able to make something along the lines of "imagery.xml" to define and load generic
18     * kml/geojson/georss feeds from multiple sources without having to code *Loader classes for each one.
19     *
20     * Oh, and it's possible to handle xml Namespaces without resorting to blanking them out of the raw data :-)
21     */
22
23     public class BikeShopLoader {
24
25         private var map:Map;
26         private var bikeShopBaseURL:String;
27         private var _layer:VectorLayer;
28         private static const STYLESHEET:String="bikeshops.css";
29
30         public function BikeShopLoader(map:Map, url:String) {
31             this.map = map;
32             this.bikeShopBaseURL = url;
33         }
34
35         public function load():void {
36             var loader:URLLoader = new URLLoader();
37             loader.load(new URLRequest(bikeShopBaseURL+"shop/missing.kml?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t));
38             loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, balls);
39             loader.addEventListener(Event.COMPLETE, parseKML);
40         }
41
42         public function balls(event:SecurityErrorEvent):void {
43             trace(event);
44         }
45
46         private function parseKML(event:Event):void {
47             //trace(event.target.data);
48             default xml namespace = new Namespace("http://www.opengis.net/kml/2.2");
49             var kml:XML = new XML(event.target.data);
50             //trace(kml.attributes());
51             //var document:XMLList = kml.Document;
52             for each (var placemark:XML in kml..Placemark) {
53               trace("name:"+placemark.name);
54               var coords:Array = placemark..coordinates.split(",");
55               var lon:Number = coords[0];
56               var lat:Number = coords[1];
57               //var ele:Number = coords[2];
58               var tags:Object = {};
59               tags["name"] = String(placemark.name);
60               tags["description"] = String(placemark.description);
61               var marker:Marker = layer.createMarker(tags, lat, lon);
62             }
63             layer.paint.updateEntityUIs(layer.getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
64         }
65
66         private function get layer():VectorLayer {
67             if (!_layer) {
68                 var n:String='Bike Shops';
69
70                 var policyFile:String = bikeShopBaseURL+"crossdomain.xml";
71                 Security.loadPolicyFile(policyFile);
72
73                 _layer=new VectorLayer(n,map,STYLESHEET);
74                 map.addVectorLayer(_layer);
75             }
76             return _layer;
77         }
78     }
79 }