/* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
* See http://svn.openlayers.org/trunk/openlayers/release-license.txt
* for the full text of the license. */
/**
* @class
*
* @requires OpenLayers/Layer/Markers.js
* @requires OpenLayers/Ajax.js
*/
OpenLayers.Layer.GPX = OpenLayers.Class.create();
OpenLayers.Layer.GPX.prototype =
OpenLayers.Class.inherit( OpenLayers.Layer.Markers, OpenLayers.Layer.Vector, {
/** store url of text file
* @type str */
url:null,
icolor:null,
/** @type Array(OpenLayers.Feature) */
features: null,
/** @type OpenLayers.Feature */
selectedFeature: null,
/**
* @constructor
*
* @param {String} name
* @param {String} location
*/
initialize: function(name, url, icolor, options) {
var newArguments = new Array()
newArguments.push(name, options);
OpenLayers.Layer.Vector.prototype.initialize.apply(this, newArguments);
OpenLayers.Layer.Markers.prototype.initialize.apply(this, [name]);
this.url = url;
this.icolor = icolor;
this.features = new Array();
var results = OpenLayers.loadURL(this.url, null, this, this.requestSuccess, this.requestFailure);
},
/**
*
*/
destroy: function() {
this.clearFeatures();
this.features = null;
OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
},
/**
* @param {XMLHttpRequest} ajaxRequest
*/
requestSuccess:function(request) {
var gpxns = "http://www.topografix.com/GPX/1/0";
var doc = request.responseXML;
if (!doc || request.fileType!="XML") {
doc = OpenLayers.parseXMLString(request.responseText);
}
if (typeof doc == "string") {
doc = OpenLayers.parseXMLString(doc);
}
/* search and display track */
// var trk = OpenLayers.Ajax.getElementsByTagNameNS(doc, gpxns, "", "trk");
var trk = doc.getElementsByTagName("trk");
var featureTRK = [];
for (var i = 0; i< trk.length; i++) {
// var color = '#00FF00'; // Fix Color
// var color=this.randomColor(); // Random Color
var color=this.icolor; // Random Color
for (var j = 0; j < trk[i].childNodes.length; j++) {
switch (trk[i].childNodes[j].nodeName)
{
case 'topografix:color':
color = '#' + OpenLayers.Util.getXmlNodeValue(trk[i].childNodes[j]);
break;
case 'trkseg':
if (color == '')
color=this.randomColor();
featureTRK.push(this.addLineGPX(trk[i].childNodes[j], color));
break;
case '#text':
break;
case 'name':
//FIXME: label the way
break;
default:
// alert('unknown ' + trk[i].childNodes[j].nodeName);
break;
}
}
}
this.addFeatures(featureTRK);
/* search and display route */
// var rte = OpenLayers.Ajax.getElementsByTagNameNS(doc, gpxns, "", "rte");
var rte = doc.getElementsByTagName("rte");
var featureRTE = [];
for (var i = 0; i< rte.length; i++) {
var color = this.randomColor();
var style_green = {
strokeColor: color,
strokeOpacity: 1,
strokeWidth: 4,
pointRadius: 6,
pointerEvents: "visiblePainted"
};
var pointList = [];
for (var j = 0; j < rte[i].childNodes.length; j++) {
switch (rte[i].childNodes[j].nodeName)
{
case 'rtept':
var feature = this.parseFeature(rte[i].childNodes[j]);
if (feature) {
pointList.push(feature);
}
break;
default:
break;
}
}
featureRTE.push(new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(pointList),null,style_green));
};
this.addFeatures(featureRTE);
/* search and display waypoint */
// var wpt = OpenLayers.Ajax.getElementsByTagNameNS(doc, gpxns, "", "wpt");
var wpt = doc.getElementsByTagName("wpt");
var featuresWPT = [];
for (var i = 0; i< wpt.length; i++) {
var data = {};
var point = this.setToMercator(wpt[i].getAttribute('lon'),wpt[i].getAttribute('lat'));
var location = new OpenLayers.LonLat(point[0], point[1]);
/* Provide defaults for title and description */
var title = "Untitled";
try {
title = OpenLayers.Util.getNodes(wpt[i],
'name')[0].firstChild.nodeValue;
}
catch (e) { title="Untitled"; }
/* GPX descriptions */
var description = "No description.";
try {
description = OpenLayers.Util.getNodes(wpt[i],
'desc')[0].firstChild.nodeValue;
}catch (e) {
var description = "No description.";
}
data.icon = OpenLayers.Marker.defaultIcon();
// data.popupSize = new OpenLayers.Size(250, 120);
if ((title != null) && (description != null)) {
// if (link) contentHTML += '';
// contentHTML += ' ' + title + '
' + title + '
' + description + '