1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * @requires OpenLayers/Layer/Vector.js
7 * @requires OpenLayers/Request/XMLHttpRequest.js
8 * @requires OpenLayers/Console.js
12 * Class: OpenLayers.Layer.GML
13 * Create a vector layer by parsing a GML file. The GML file is
14 * passed in as a parameter.
17 * - <OpenLayers.Layer.Vector>
19 OpenLayers.Layer.GML = OpenLayers.Class(OpenLayers.Layer.Vector, {
23 * {Boolean} Flag for whether the GML data has been loaded yet.
29 * {<OpenLayers.Format>} The format you want the data to be parsed with.
34 * APIProperty: formatOptions
35 * {Object} Hash of options which should be passed to the format when it is
36 * created. Must be passed in the constructor.
41 * Constructor: OpenLayers.Layer.GML
42 * Load and parse a single file on the web, according to the format
43 * provided via the 'format' option, defaulting to GML.
47 * url - {String} URL of a GML file.
48 * options - {Object} Hashtable of extra options to tag onto the layer.
50 initialize: function(name, url, options) {
51 var newArguments = [];
52 newArguments.push(name, options);
53 OpenLayers.Layer.Vector.prototype.initialize.apply(this, newArguments);
58 * APIMethod: setVisibility
59 * Set the visibility flag for the layer and hide/show&redraw accordingly.
60 * Fire event unless otherwise specified
61 * GML will be loaded if the layer is being made visible for the first
65 * visible - {Boolean} Whether or not to display the layer
69 setVisibility: function(visibility, noEvent) {
70 OpenLayers.Layer.Vector.prototype.setVisibility.apply(this, arguments);
71 if(this.visibility && !this.loaded){
79 * If layer is visible and GML has not been loaded, load GML, then load GML
80 * and call OpenLayers.Layer.Vector.moveTo() to redraw at the new location.
84 * zoomChanged - {Object}
87 moveTo:function(bounds, zoomChanged, minor) {
88 OpenLayers.Layer.Vector.prototype.moveTo.apply(this, arguments);
89 // Wait until initialisation is complete before loading GML
90 // otherwise we can get a race condition where the root HTML DOM is
91 // loaded after the GML is paited.
92 // See http://trac.openlayers.org/ticket/404
93 if(this.visibility && !this.loaded){
101 loadGML: function() {
103 this.events.triggerEvent("loadstart");
104 OpenLayers.Request.GET({
106 success: this.requestSuccess,
107 failure: this.requestFailure,
116 * Change the URL and reload the GML
119 * url - {String} URL of a GML file.
121 setUrl:function(url) {
123 this.destroyFeatures();
129 * Method: requestSuccess
130 * Process GML after it has been loaded.
131 * Called by initialise() and loadUrl() after the GML has been loaded.
136 requestSuccess:function(request) {
137 var doc = request.responseXML;
139 if (!doc || !doc.documentElement) {
140 doc = request.responseText;
145 OpenLayers.Util.extend(options, this.formatOptions);
146 if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
147 options.externalProjection = this.projection;
148 options.internalProjection = this.map.getProjectionObject();
151 var gml = this.format ? new this.format(options) : new OpenLayers.Format.GML(options);
152 this.addFeatures(gml.read(doc));
153 this.events.triggerEvent("loadend");
157 * Method: requestFailure
158 * Process a failed loading of GML.
159 * Called by initialise() and loadUrl() if there was a problem loading GML.
164 requestFailure: function(request) {
165 OpenLayers.Console.userError(OpenLayers.i18n("errorLoadingGML", {'url':this.url}));
166 this.events.triggerEvent("loadend");
169 CLASS_NAME: "OpenLayers.Layer.GML"