Text.js
6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* 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
*/
OpenLayers.Layer.Text = OpenLayers.Class.create();
OpenLayers.Layer.Text.prototype =
OpenLayers.Class.inherit( OpenLayers.Layer.Markers, {
/** store url of text file - this should be specified in the
* "options" hashtable
* @type str */
location:null,
/** @type Array(OpenLayers.Feature) */
features: null,
/** @type OpenLayers.Feature */
selectedFeature: null,
/**
* @constructor
*
* @param {String} name
* @param {String} location
* @param {Object} options Hashtable of extra options to tag onto the layer
*/
initialize: function(name, options) {
OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
this.features = new Array();
if (this.location != null) {
OpenLayers.loadURL(this.location, null, this, this.parseData);
}
},
/**
*
*/
destroy: function() {
this.clearFeatures();
this.features = null;
OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
},
/**
* @param {XMLHttpRequest} ajaxRequest
*/
parseData: function(ajaxRequest) {
var text = ajaxRequest.responseText;
var lines = text.split('\n');
var columns;
// length - 1 to allow for trailing new line
for (var lcv = 0; lcv < (lines.length - 1); lcv++) {
var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,'');
if (currLine.charAt(0) != '#') { /* not a comment */
if (!columns) {
//First line is columns
columns = currLine.split('\t');
} else {
var vals = currLine.split('\t');
var location = new OpenLayers.LonLat(0,0);
var title; var url;
var icon, iconSize, iconOffset;
var set = false;
for (var valIndex = 0; valIndex < vals.length; valIndex++) {
if (vals[valIndex]) {
if (columns[valIndex] == 'point') {
var coords = vals[valIndex].split(',');
location.lat = parseFloat(coords[0]);
location.lon = parseFloat(coords[1]);
set = true;
} else if (columns[valIndex] == 'lat') {
location.lat = parseFloat(vals[valIndex]);
set = true;
} else if (columns[valIndex] == 'lon') {
location.lon = parseFloat(vals[valIndex]);
set = true;
} else if (columns[valIndex] == 'title')
title = vals[valIndex];
else if (columns[valIndex] == 'image' ||
columns[valIndex] == 'icon')
url = vals[valIndex];
else if (columns[valIndex] == 'iconSize') {
var size = vals[valIndex].split(',');
iconSize = new OpenLayers.Size(parseFloat(size[0]),
parseFloat(size[1]));
} else if (columns[valIndex] == 'iconOffset') {
var offset = vals[valIndex].split(',');
iconOffset = new OpenLayers.Pixel(parseFloat(offset[0]),
parseFloat(offset[1]));
} else if (columns[valIndex] == 'title') {
title = vals[valIndex];
} else if (columns[valIndex] == 'description') {
description = vals[valIndex];
}
}
}
if (set) {
var data = new Object();
if (url != null) {
data.icon = new OpenLayers.Icon(url,
iconSize,
iconOffset);
} else {
data.icon = OpenLayers.Marker.defaultIcon();
//allows for the case where the image url is not
// specified but the size is. use a default icon
// but change the size
if (iconSize != null) {
data.icon.setSize(iconSize);
}
}
if ((title != null) && (description != null)) {
data['popupContentHTML'] = '<h2>'+title+'</h2><p>'+description+'</p>';
}
var feature = new OpenLayers.Feature(this, location, data);
this.features.push(feature);
var marker = feature.createMarker();
if ((title != null) && (description != null)) {
marker.events.register('click', feature, this.markerClick);
}
this.addMarker(marker);
}
}
}
}
},
/**
* @param {Event} evt
*/
markerClick: function(evt) {
sameMarkerClicked = (this == this.layer.selectedFeature);
this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
for(var i=0; i < this.layer.map.popups.length; i++) {
this.layer.map.removePopup(this.layer.map.popups[i]);
}
if (!sameMarkerClicked) {
this.layer.map.addPopup(this.createPopup());
}
OpenLayers.Event.stop(evt);
},
/**
*
*/
clearFeatures: function() {
if (this.features != null) {
while(this.features.length > 0) {
var feature = this.features[0];
OpenLayers.Util.removeItem(this.features, feature);
feature.destroy();
}
}
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Layer.Text"
});