Working text layer with popups! woohoo! parseText suffered a reindentation, so not as much changed here as it looks like.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@424 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2006-05-27 13:11:22 +00:00
parent 1408a26b58
commit 82d01c73cc
+70 -52
View File
@@ -21,60 +21,78 @@ OpenLayers.Layer.Text.prototype =
{ method: 'get', onComplete:this.parseData.bind(this) } ); { method: 'get', onComplete:this.parseData.bind(this) } );
}, },
parseData: function(ajaxRequest) { parseData: function(ajaxRequest) {
var text = ajaxRequest.responseText; var text = ajaxRequest.responseText;
var lines = text.split('\n'); var lines = text.split('\n');
var columns; var columns;
// length - 1 to allow for trailing new line // length - 1 to allow for trailing new line
for (var lcv = 0; lcv < (lines.length - 1); lcv++) { for (var lcv = 0; lcv < (lines.length - 1); lcv++) {
var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,''); var currLine = lines[lcv].replace(/^\s*/,'').replace(/\s*$/,'');
if (currLine.charAt(0) != '#') { /* not a comment */ if (currLine.charAt(0) != '#') { /* not a comment */
if (!columns) { if (!columns) {
//First line is columns //First line is columns
columns = currLine.split('\t'); columns = currLine.split('\t');
} else { } else {
var vals = currLine.split('\t'); var vals = currLine.split('\t');
var location = new OpenLayers.LonLat(0,0); var location = new OpenLayers.LonLat(0,0);
var title = ""; var description = ""; var title; var url;
var icon = new OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png',new OpenLayers.Size(10,17)); var icon, iconSize, iconOffset;
var set = false; var set = false;
for (var valIndex = 0; valIndex < vals.length; valIndex++) { for (var valIndex = 0; valIndex < vals.length; valIndex++) {
if (vals[valIndex]) { if (vals[valIndex]) {
if (columns[valIndex] == 'point') { if (columns[valIndex] == 'point') {
var coords = vals[valIndex].split(','); var coords = vals[valIndex].split(',');
location.lat = parseFloat(coords[0]); location.lat = parseFloat(coords[0]);
location.lon = parseFloat(coords[1]); location.lon = parseFloat(coords[1]);
set = true; set = true;
} else if (columns[valIndex] == 'lat') { } else if (columns[valIndex] == 'lat') {
location.lat = parseFloat(vals[valIndex]); location.lat = parseFloat(vals[valIndex]);
set = true; set = true;
} else if (columns[valIndex] == 'lon') { } else if (columns[valIndex] == 'lon') {
location.lon = parseFloat(vals[valIndex]); location.lon = parseFloat(vals[valIndex]);
set = true; set = true;
} else if (columns[valIndex] == 'title') } else if (columns[valIndex] == 'title')
title = vals[valIndex]; title = vals[valIndex];
else if (columns[valIndex] == 'image') else if (columns[valIndex] == 'image' ||
icon.url = vals[valIndex]; columns[valIndex] == 'icon')
else if (columns[valIndex] == 'title') url = vals[valIndex];
title = vals[valIndex]; else if (columns[valIndex] == 'iconSize') {
else if (columns[valIndex] == 'description') { var size = vals[valIndex].split(',');
description = vals[valIndex]; iconSize = new OpenLayers.Size(parseFloat(size[0]),
} parseFloat(size[1]));
} } else if (columns[valIndex] == 'iconOffset') {
} var offset = vals[valIndex].split(',');
if (set) { iconOffset = new OpenLayers.Pixel(parseFloat(offset[0]),
var marker = new OpenLayers.Marker(location, icon); parseFloat(offset[1]));
var popup = new OpenLayers.Popup(null, location, null, '<h2>'+title+'</h2><p>'+description+'</p>'); } else if (columns[valIndex] == 'title') {
marker.events.register('click', this, function(evt) { this.map.addPopup(popup); Event.stop(evt); } ); //this.map.addPopup(popup); }); title = vals[valIndex];
this.addMarker(marker); } else if (columns[valIndex] == 'description') {
} description = vals[valIndex];
} }
} }
}
if (set) {
var data = {};
data['iconURL'] = url;
data['iconSize'] = iconSize;
data['iconOffset'] = iconOffset;
data['popupContentHTML'] = '<h2>'+title+'</h2><p>'+description+'</p>';
var feature = new OpenLayers.Feature(this, location, data);
var marker = feature.createMarker();
marker.events.register('click', feature, this.markerClick);
this.addMarker(marker);
}
}
}
} }
}, },
testFunction: function() { markerClick: function(evt) {
alert('tesT'); for(var i=0; i < this.layer.map.popups.length; i++) {
this.layer.map.removePopup(this.layer.map.popups[i]);
}
this.layer.map.addPopup(this.createPopup());
Event.stop(evt);
} }
}); });