Update trackloader to avoid using VectorLayer
authorAndy Allan <andy@gravitystorm.co.uk>
Mon, 23 May 2011 11:22:08 +0000 (12:22 +0100)
committerAndy Allan <andy@gravitystorm.co.uk>
Mon, 23 May 2011 11:22:08 +0000 (12:22 +0100)
net/systemeD/halcyon/Map.as
net/systemeD/potlatch2/utils/TrackLoader.as

index e24a6ce..1c79093 100644 (file)
@@ -245,10 +245,11 @@ package net.systemeD.halcyon {
                // ------------------------------------------------------------------------------------------
                // Add layers
                
-               public function addLayer(connection:Connection, styleurl:String, backgroundlayer:Boolean=true):void {
+               public function addLayer(connection:Connection, styleurl:String, backgroundlayer:Boolean=true):MapPaint {
                        var paint:MapPaint=new MapPaint(this, connection, styleurl, -5, 5);
                        paintContainer.addChild(paint);
                        paint.isBackground=backgroundlayer;
+                       return paint;
                }
 
                public function removeLayerByName(name:String):void {
index 2835030..25b3b75 100644 (file)
@@ -2,7 +2,7 @@ package net.systemeD.potlatch2.utils {
 
        import net.systemeD.halcyon.connection.*;
        import net.systemeD.halcyon.Map;
-       import net.systemeD.halcyon.VectorLayer;
+       import net.systemeD.halcyon.MapPaint;
        import flash.net.*;
        import flash.events.*;
 
@@ -20,12 +20,15 @@ package net.systemeD.potlatch2.utils {
 
                private var map:Map;
                private var apiBaseURL:String;
+               private var connection:Connection; /* to store the nodes/ways that are faked up for GPX tracks */
 
                private static const STYLESHEET:String="stylesheets/gpx.css";
+               private static const LAYER:String="GPS tracks";
                
                public function TrackLoader(map:Map, url:String) {
                        this.map=map;
                        apiBaseURL=url;
+                       connection = new Connection(LAYER,apiBaseURL,null, null);
                }
                
                public function load(keep:Boolean=false):void {
@@ -37,9 +40,10 @@ package net.systemeD.potlatch2.utils {
                                top   =map.edge_t;
                                bottom=map.edge_b;
                                page=0;
-                               if (!keep) { } // ** TODO: blank the vector layer
+                               if (!keep) { } // ** TODO: blank the connection objects
                        }
 
+            /* This isn't great - conceptially it would be nicer for the connection to do the request */
                        var loader:URLLoader = new URLLoader();
                        loader.load(new URLRequest(apiBaseURL+"trackpoints?bbox="+left+","+bottom+","+right+","+top+"&page="+page));
                        loader.addEventListener(Event.COMPLETE, parseGPX);
@@ -47,6 +51,7 @@ package net.systemeD.potlatch2.utils {
 
                public function parseGPX(event:Event):void {
                        var file:XML = new XML(event.target.data);
+                       var action:CompositeUndoableAction = new CompositeUndoableAction("add track objects");
                        for each (var ns:Namespace in file.namespaceDeclarations()) {
                                if (ns.uri.match(/^http:\/\/www\.topografix\.com\/GPX\/1\/[01]$/)) {
                                        default xml namespace = ns;
@@ -61,28 +66,26 @@ package net.systemeD.potlatch2.utils {
                                        lat = trkpt.@lat;
                     lon = trkpt.@lon;
                     if (lastlat && nodestring.length > 0 && greatCircle(lat, lon, lastlat, lastlon) > 30) {
-                        layer.createWay({}, nodestring);
+                        connection.createWay({}, nodestring, action.push);
                         nodestring = [];
                     }
-                    nodestring.push(layer.createNode({}, lat, lon));
+                    nodestring.push(connection.createNode({}, lat, lon, action.push));
                     lastlat = lat; lastlon = lon;
                                }
-                if (nodestring.length > 0) { layer.createWay({}, nodestring); }
+                if (nodestring.length > 0) { connection.createWay({}, nodestring, action.push); trace("create way");}
                        }
-            
+
+            action.doAction(); /* just do it, don't add to undo stack */
                        default xml namespace = new Namespace("");
-                       layer.paint.updateEntityUIs(false, false);
+                       layer.updateEntityUIs(false, false);
                }
 
-               private function get layer():VectorLayer {
-                       // >>>> REFACTOR: VectorLayer commented out
-                       // var vl:VectorLayer=map.findVectorLayer('GPS tracks');
-                       // if (!vl) {
-                       //      vl=new VectorLayer('GPS tracks',map,STYLESHEET);
-                       //      map.addVectorLayer(vl);
-                       // }
-                       // return vl;
-                       return null;
+               private function get layer():MapPaint {
+            var mp:MapPaint = map.findLayer(LAYER);
+            if (!mp) {
+                mp = map.addLayer(connection, STYLESHEET);
+            }
+            return mp;
                }
                
                private function greatCircle(lat1:Number,lon1:Number,lat2:Number,lon2:Number):Number {