move stylesheets into dedicated directory
[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 name:String;
28         private var _layer:VectorLayer;
29         private static const STYLESHEET:String="stylesheets/bikeshops.css";
30
31         public function BikeShopLoader(map:Map, url:String, name:String) {
32             this.map = map;
33             this.bikeShopBaseURL = url;
34             this.name = name;
35         }
36
37         public function load():void {
38             var loader:URLLoader = new URLLoader();
39             loader.load(new URLRequest(bikeShopBaseURL+"shop/missing.kml?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t));
40             loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, balls);
41             loader.addEventListener(Event.COMPLETE, parseKML);
42         }
43
44         public function balls(event:SecurityErrorEvent):void {
45             trace(event);
46         }
47
48         private function parseKML(event:Event):void {
49             //trace(event.target.data);
50             default xml namespace = new Namespace("http://www.opengis.net/kml/2.2");
51             var kml:XML = new XML(event.target.data);
52             //trace(kml.attributes());
53             //var document:XMLList = kml.Document;
54             for each (var placemark:XML in kml..Placemark) {
55               trace("name:"+placemark.name);
56               var coords:Array = placemark..coordinates.split(",");
57               var lon:Number = coords[0];
58               var lat:Number = coords[1];
59               //var ele:Number = coords[2];
60               var tags:Object = {};
61               tags["name"] = String(placemark.name);
62               tags["description"] = String(placemark.description);
63               var marker:Marker = layer.createMarker(tags, lat, lon);
64             }
65                         default xml namespace = new Namespace("");
66             layer.paint.updateEntityUIs(layer.getObjectsByBbox(map.edge_l,map.edge_r,map.edge_t,map.edge_b), true, false);
67         }
68
69         private function get layer():VectorLayer {
70             if (!_layer) {
71                 var policyFile:String = bikeShopBaseURL+"crossdomain.xml";
72                 Security.loadPolicyFile(policyFile);
73
74                 _layer=new VectorLayer(name,map,STYLESHEET);
75                 map.addVectorLayer(_layer);
76             }
77             return _layer;
78         }
79     }
80 }