1 /* Copyright (c) 2006-2007 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/Console.js
11 * Class: OpenLayers.Layer.PointTrack
12 * Vector layer to display ordered point features as a line, creating one
13 * LineString feature for each pair of two points.
16 * - <OpenLayers.Layer.Vector>
18 OpenLayers.Layer.PointTrack = OpenLayers.Class(OpenLayers.Layer.Vector, {
22 * dataFrom - {<OpenLayers.Layer.PointTrack.dataFrom>} optional. If the
23 * lines should get the data/attributes from one of the two
24 * points, creating it, which one should it be?
29 * Constructor: OpenLayers.PointTrack
30 * Constructor for a new OpenLayers.PointTrack instance.
33 * name - {String} name of the layer
34 * options - {Object} Optional object with properties to tag onto the
37 initialize: function(name, options) {
38 OpenLayers.Layer.Vector.prototype.initialize.apply(this, arguments);
43 * Adds point features that will be used to create lines from, using point
44 * pairs. The first point of a pair will be the source node, the second
45 * will be the target node.
48 * pointFeatures - {Array(<OpenLayers.Feature>)}
51 addNodes: function(pointFeatures) {
52 if (pointFeatures.length < 2) {
53 OpenLayers.Console.error(
54 "At least two point features have to be added to create" +
59 var lines = new Array(pointFeatures.length-1);
61 var pointFeature, startPoint, endPoint;
62 for(var i=0, len=pointFeatures.length; i<len; i++) {
63 pointFeature = pointFeatures[i];
64 endPoint = pointFeature.geometry;
67 var lonlat = pointFeature.lonlat;
68 endPoint = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
69 } else if(endPoint.CLASS_NAME != "OpenLayers.Geometry.Point") {
70 OpenLayers.Console.error(
71 "Only features with point geometries are supported.");
76 var attributes = (this.dataFrom != null) ?
77 (pointFeatures[i+this.dataFrom].data ||
78 pointFeatures[i+this.dataFrom].attributes) :
80 var line = new OpenLayers.Geometry.LineString([startPoint,
83 lines[i-1] = new OpenLayers.Feature.Vector(line, attributes);
86 startPoint = endPoint;
89 this.addFeatures(lines);
92 CLASS_NAME: "OpenLayers.Layer.PointTrack"
96 * Constant: OpenLayers.Layer.PointTrack.dataFrom
97 * {Object} with the following keys
98 * - SOURCE_NODE: take data/attributes from the source node of the line
99 * - TARGET_NODE: take data/attributes from the target node of the line
101 OpenLayers.Layer.PointTrack.dataFrom = {'SOURCE_NODE': -1, 'TARGET_NODE': 0};