[OpenLayers-Commits] r12359 - in sandbox/camptocamp/gpx/openlayers:
lib/OpenLayers/Format tests/Format
commits-20090109 at openlayers.org
commits-20090109 at openlayers.org
Mon Sep 12 07:43:23 EDT 2011
Author: pgiraud
Date: 2011-09-12 04:43:22 -0700 (Mon, 12 Sep 2011)
New Revision: 12359
Modified:
sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js
sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html
Log:
Adding support for metadata
Modified: sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js
===================================================================
--- sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js 2011-09-12 04:31:16 UTC (rev 12358)
+++ sandbox/camptocamp/gpx/openlayers/lib/OpenLayers/Format/GPX.js 2011-09-12 11:43:22 UTC (rev 12359)
@@ -210,8 +210,10 @@
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)} List of features to serialize into a string.
+ * metadata - {Object} A key/value pairs object to build a metadata node to
+ * add to the gpx. Supported keys are 'name', 'desc', 'author'.
*/
- write: function(features) {
+ write: function(features, metadata) {
var gpx;
features = OpenLayers.Util.isArray(features) ?
features : [features];
@@ -220,10 +222,34 @@
for(var i=0, len=features.length; i<len; i++) {
gpx.appendChild(this.buildFeatureNode(features[i]));
}
+ if (metadata && typeof metadata == 'object') {
+ gpx.appendChild(this.buildMetadataNode(metadata));
+ }
return OpenLayers.Format.XML.prototype.write.apply(this, [gpx]);
},
/**
+ * Method: buildMetadataNode
+ * Creates a "metadata" node.
+ *
+ * Returns:
+ * {DOMElement}
+ */
+ buildMetadataNode: function(metadata) {
+ var types = ['name', 'desc', 'author'],
+ node = this.createElementNS(this.gpxns, 'metadata');
+ for (var i=0; i < types.length; i++) {
+ var type = types[i];
+ if (metadata[type]) {
+ var n = this.createElementNS(this.gpxns, type);
+ n.appendChild(this.createTextNode(metadata[type]));
+ node.appendChild(n);
+ }
+ }
+ return node;
+ },
+
+ /**
* Method: buildFeatureNode
* Accepts an <OpenLayers.Feature.Vector>, and builds a node for it.
*
@@ -231,7 +257,7 @@
* feature - {<OpenLayers.Feature.Vector>}
*
* Returns:
- * {Array(DOMElement)}
+ * {DOMElement} - The created node, either a 'wpt' or a 'trk'.
*/
buildFeatureNode: function(feature) {
var geometry = feature.geometry;
Modified: sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html
===================================================================
--- sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html 2011-09-12 04:31:16 UTC (rev 12358)
+++ sandbox/camptocamp/gpx/openlayers/tests/Format/GPX.html 2011-09-12 11:43:22 UTC (rev 12359)
@@ -105,6 +105,14 @@
var data = parser.write([f]);
t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><trk><name>foo</name><desc>bar</desc><trkseg><trkpt lon="-111.04" lat="45.68"/><trkpt lon="-112.04" lat="45.68"/><trkpt lon="-111.04" lat="45.68"/></trkseg></trk></gpx>', 'GPX serializes polygon correctly');
}
+ function test_Format_GPX_serialize_metadata(t) {
+ t.plan(1);
+
+ var parser = new OpenLayers.Format.GPX();
+
+ var data = parser.write([], {name: 'foo', desc: 'bar'});
+ t.xml_eq(data, '<?xml version="1.0" encoding="ISO-8859-1"?><gpx xmlns="http://www.topografix.com/GPX/1/1"><metadata><name>foo</name><desc>bar</desc></metadata></gpx>', 'GPX serializes metadata correctly');
+ }
</script>
</head>
<body>
More information about the Commits
mailing list