Draw new markers when they are created, and save the data loaders from worrying about...
[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.MapPaint;
5     import net.systemeD.halcyon.connection.Connection;
6     import net.systemeD.halcyon.connection.Marker;
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:MapPaint;
29         private var connection:Connection;
30         private static const STYLESHEET:String="stylesheets/bikeshops.css";
31
32         public function BikeShopLoader(map:Map, url:String, name:String) {
33             this.map = map;
34             this.bikeShopBaseURL = url;
35             this.name = name;
36             this.connection = new Connection(name,url,null,null);
37         }
38
39         public function load():void {
40             // this would be better if the connection did the loading
41             var loader:URLLoader = new URLLoader();
42             loader.load(new URLRequest(bikeShopBaseURL+"shop/missing.kml?bbox="+map.edge_l+","+map.edge_b+","+map.edge_r+","+map.edge_t));
43             loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, balls);
44             loader.addEventListener(Event.COMPLETE, parseKML);
45         }
46
47         public function balls(event:SecurityErrorEvent):void {
48             trace(event);
49         }
50
51         private function parseKML(event:Event):void {
52             //trace(event.target.data);
53             default xml namespace = new Namespace("http://www.opengis.net/kml/2.2");
54             var kml:XML = new XML(event.target.data);
55             //trace(kml.attributes());
56             //var document:XMLList = kml.Document;
57             for each (var placemark:XML in kml..Placemark) {
58               trace("name:"+placemark.name);
59               var coords:Array = placemark..coordinates.split(",");
60               var lon:Number = coords[0];
61               var lat:Number = coords[1];
62               //var ele:Number = coords[2];
63               var tags:Object = {};
64               tags["name"] = String(placemark.name);
65               tags["description"] = String(placemark.description);
66               var marker:Marker = connection.createMarker(tags, lat, lon);
67             }
68                         default xml namespace = new Namespace("");
69             layer.updateEntityUIs(true, false);
70         }
71
72         private function get layer():MapPaint {
73             if (!_layer) {
74               // this should be done by the connection, not here.
75               var policyFile:String = bikeShopBaseURL+"crossdomain.xml";
76               Security.loadPolicyFile(policyFile);
77
78               _layer = map.addLayer(connection, STYLESHEET);
79             }
80             return _layer;
81         }
82     }
83 }