b2d839b4d8217d06647c02929b292a4af9336982.svn-base
9.34 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Layer/Markers.js
* @requires OpenLayers/Format/Text.js
* @requires OpenLayers/Request/XMLHttpRequest.js
*/
/**
* Class: OpenLayers.Layer.Text
* This layer creates markers given data in a text file. The <location>
* property of the layer (specified as a property of the options argument
* in the <OpenLayers.Layer.Text> constructor) points to a tab delimited
* file with data used to create markers.
*
* The first row of the data file should be a header line with the column names
* of the data. Each column should be delimited by a tab space. The
* possible columns are:
* - *point* lat,lon of the point where a marker is to be placed
* - *lat* Latitude of the point where a marker is to be placed
* - *lon* Longitude of the point where a marker is to be placed
* - *icon* or *image* URL of marker icon to use.
* - *iconSize* Size of Icon to use.
* - *iconOffset* Where the top-left corner of the icon is to be placed
* relative to the latitude and longitude of the point.
* - *title* The text of the 'title' is placed inside an 'h2' marker
* inside a popup, which opens when the marker is clicked.
* - *description* The text of the 'description' is placed below the h2
* in the popup. this can be plain text or HTML.
*
* Example text file:
* (code)
* lat lon title description iconSize iconOffset icon
* 10 20 title description 21,25 -10,-25 http://www.openlayers.org/dev/img/marker.png
* (end)
*
* Inherits from:
* - <OpenLayers.Layer.Markers>
*/
OpenLayers.Layer.Text = OpenLayers.Class(OpenLayers.Layer.Markers, {
/**
* APIProperty: location
* {String} URL of text file. Must be specified in the "options" argument
* of the constructor. Can not be changed once passed in.
*/
location:null,
/**
* Property: features
* {Array(<OpenLayers.Feature>)}
*/
features: null,
/**
* APIProperty: formatOptions
* {Object} Hash of options which should be passed to the format when it is
* created. Must be passed in the constructor.
*/
formatOptions: null,
/**
* Property: selectedFeature
* {<OpenLayers.Feature>}
*/
selectedFeature: null,
/**
* Constructor: OpenLayers.Layer.Text
* Create a text layer.
*
* Parameters:
* name - {String}
* options - {Object} Object with properties to be set on the layer.
* Must include <location> property.
*/
initialize: function(name, options) {
OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
this.features = new Array();
},
/**
* APIMethod: destroy
*/
destroy: function() {
// Warning: Layer.Markers.destroy() must be called prior to calling
// clearFeatures() here, otherwise we leak memory. Indeed, if
// Layer.Markers.destroy() is called after clearFeatures(), it won't be
// able to remove the marker image elements from the layer's div since
// the markers will have been destroyed by clearFeatures().
OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
this.clearFeatures();
this.features = null;
},
/**
* Method: loadText
* Start the load of the Text data. Don't do this when we first add the layer,
* since we may not be visible at any point, and it would therefore be a waste.
*/
loadText: function() {
if (!this.loaded) {
if (this.location != null) {
var onFail = function(e) {
this.events.triggerEvent("loadend");
};
this.events.triggerEvent("loadstart");
OpenLayers.Request.GET({
url: this.location,
success: this.parseData,
failure: onFail,
scope: this
});
this.loaded = true;
}
}
},
/**
* Method: moveTo
* If layer is visible and Text has not been loaded, load Text.
*
* Parameters:
* bounds - {Object}
* zoomChanged - {Object}
* minor - {Object}
*/
moveTo:function(bounds, zoomChanged, minor) {
OpenLayers.Layer.Markers.prototype.moveTo.apply(this, arguments);
if(this.visibility && !this.loaded){
this.loadText();
}
},
/**
* Method: parseData
*
* Parameters:
* ajaxRequest - {<OpenLayers.Request.XMLHttpRequest>}
*/
parseData: function(ajaxRequest) {
var text = ajaxRequest.responseText;
var options = {};
OpenLayers.Util.extend(options, this.formatOptions);
if (this.map && !this.projection.equals(this.map.getProjectionObject())) {
options.externalProjection = this.projection;
options.internalProjection = this.map.getProjectionObject();
}
var parser = new OpenLayers.Format.Text(options);
var features = parser.read(text);
for (var i=0, len=features.length; i<len; i++) {
var data = {};
var feature = features[i];
var location;
var iconSize, iconOffset;
location = new OpenLayers.LonLat(feature.geometry.x,
feature.geometry.y);
if (feature.style.graphicWidth
&& feature.style.graphicHeight) {
iconSize = new OpenLayers.Size(
feature.style.graphicWidth,
feature.style.graphicHeight);
}
// FIXME: At the moment, we only use this if we have an
// externalGraphic, because icon has no setOffset API Method.
/**
* FIXME FIRST!!
* The Text format does all sorts of parseFloating
* The result of a parseFloat for a bogus string is NaN. That
* means the three possible values here are undefined, NaN, or a
* number. The previous check was an identity check for null. This
* means it was failing for all undefined or NaN. A slightly better
* check is for undefined. An even better check is to see if the
* value is a number (see #1441).
*/
if (feature.style.graphicXOffset !== undefined
&& feature.style.graphicYOffset !== undefined) {
iconOffset = new OpenLayers.Pixel(
feature.style.graphicXOffset,
feature.style.graphicYOffset);
}
if (feature.style.externalGraphic != null) {
data.icon = new OpenLayers.Icon(feature.style.externalGraphic,
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 ((feature.attributes.title != null)
&& (feature.attributes.description != null)) {
data['popupContentHTML'] =
'<h2>'+feature.attributes.title+'</h2>' +
'<p>'+feature.attributes.description+'</p>';
}
data['overflow'] = feature.attributes.overflow || "auto";
var markerFeature = new OpenLayers.Feature(this, location, data);
this.features.push(markerFeature);
var marker = markerFeature.createMarker();
if ((feature.attributes.title != null)
&& (feature.attributes.description != null)) {
marker.events.register('click', markerFeature, this.markerClick);
}
this.addMarker(marker);
}
this.events.triggerEvent("loadend");
},
/**
* Property: markerClick
*
* Parameters:
* evt - {Event}
*/
markerClick: function(evt) {
var sameMarkerClicked = (this == this.layer.selectedFeature);
this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
for(var i=0, len=this.layer.map.popups.length; i<len; i++) {
this.layer.map.removePopup(this.layer.map.popups[i]);
}
if (!sameMarkerClicked) {
this.layer.map.addPopup(this.createPopup());
}
OpenLayers.Event.stop(evt);
},
/**
* Method: clearFeatures
*/
clearFeatures: function() {
if (this.features != null) {
while(this.features.length > 0) {
var feature = this.features[0];
OpenLayers.Util.removeItem(this.features, feature);
feature.destroy();
}
}
},
CLASS_NAME: "OpenLayers.Layer.Text"
});