]> git.openstreetmap.org Git - rails.git/blob - public/lib/OpenLayers/Tile/WFS.js
openlayers madness
[rails.git] / public / lib / OpenLayers / Tile / WFS.js
1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
2  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
3  * text of the license. */
4 // @require: OpenLayers/Tile.js
5 /**
6 * @class
7 */
8 OpenLayers.Tile.WFS = Class.create();
9 OpenLayers.Tile.WFS.prototype = 
10   Object.extend( new OpenLayers.Tile(), {
11
12     /** @type Array(OpenLayers.Feature)*/ 
13     features: null,
14
15
16     /** 
17     * @constructor
18     *
19     * @param {OpenLayers.Layer} layer
20     * @param {OpenLayers.Pixel} position
21     * @param {OpenLayers.Bounds} bounds
22     * @param {String} url
23     * @param {OpenLayers.Size} size
24     */
25     initialize: function(layer, position, bounds, url, size) {
26         OpenLayers.Tile.prototype.initialize.apply(this, arguments);
27         
28         this.features = new Array();
29     },
30
31     /**
32      * 
33      */
34     destroy: function() {
35         for(var i=0; i < this.features.length; i++) {
36             this.features[i].destroy();
37         }
38         OpenLayers.Tile.prototype.destroy.apply(this, arguments);
39     },
40
41     /**
42     */
43     draw:function() {
44         this.loadFeaturesForRegion(this.requestSuccess);        
45     },
46
47     
48     /** get the full request string from the ds and the tile params 
49     *     and call the AJAX loadURL(). 
50     *
51     *     input are function pointers for what to do on success and failure.
52     * 
53     * @param {function} success
54     * @param {function} failure
55     */
56     loadFeaturesForRegion:function(success, failure) {
57
58         if (!this.loaded) {
59         
60             if (this.url != "") {
61         
62                 // TODO: Hmmm, this stops multiple loads of the data when a 
63                 //       result isn't immediately retrieved, but it's hacky. 
64                 //       Do it better.
65                 this.loaded = true; 
66                 OpenLayers.loadURL(this.url, null, this, success, failure);
67             }
68         }
69     },
70     
71     /** Return from AJAX request
72     *
73     * @param {} request
74     */
75     requestSuccess:function(request) {
76         var doc = request.responseXML;
77         
78         if (!doc || request.fileType!="XML") {
79             doc = OpenLayers.parseXMLString(request.responseText);
80         }
81         
82         var resultFeatures = doc.getElementsByTagName("featureMember");
83             
84         //clear old featureList
85         this.features = new Array();
86
87         for (var i=0; i < resultFeatures.length; i++) {
88         
89             var feature = new this.layer.featureClass(this.layer, 
90                                                       resultFeatures[i]);
91             this.features.append(feature);
92         }
93         
94     },
95
96     /** @final @type String */
97     CLASS_NAME: "OpenLayers.Tile.WFS"
98   }
99 );
100