From 519fb6da15320fd3916a5959cdff0aa3d0c1ce1b Mon Sep 17 00:00:00 2001 From: crschmidt Date: Fri, 19 May 2006 02:26:00 +0000 Subject: [PATCH] Add working version of text file layer. (For those of you playing along at home, this was about 45 minutes of work -- part of which was trying to figure out why the data file wouldn't work, only to find out I had expandtab on in vim -- so I wasn't typing tabs, i was typing spaces!) This has uncovered the fact that there is some logical error in our code somewhere: Visit markers.html then click the purple marker repeatedly. Despite the fact that you start clicking in the sea, the marker moves north, as does your center point. This text file layer does not yet support popups or anything else, just drawing the images at a lat/lon with an 'image' given in the textfile. git-svn-id: http://svn.openlayers.org/trunk/openlayers@172 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers.js | 1 + lib/OpenLayers/Layer/Text.js | 73 ++++++++++++++++++++++++++++++++++++ markers.html | 2 + textfile.txt | 4 ++ 4 files changed, 80 insertions(+) create mode 100644 lib/OpenLayers/Layer/Text.js create mode 100644 textfile.txt diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index e36ee053f2..5fa4fe16b4 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -48,6 +48,7 @@ catch(e){ "OpenLayers/Layer/Google.js", "OpenLayers/Layer/Grid.js", "OpenLayers/Layer/Marker.js", + "OpenLayers/Layer/Text.js", "OpenLayers/Layer/WMS.js", "OpenLayers/Control.js", "OpenLayers/Control/PanZoom.js", diff --git a/lib/OpenLayers/Layer/Text.js b/lib/OpenLayers/Layer/Text.js new file mode 100644 index 0000000000..47a0ad8894 --- /dev/null +++ b/lib/OpenLayers/Layer/Text.js @@ -0,0 +1,73 @@ +/** +* @class +*/ +OpenLayers.Layer.Text = Class.create(); +OpenLayers.Layer.Text.prototype = + Object.extend( new OpenLayers.Layer.Marker(), { + + /** store url of text file + * @type str */ + location:null, + /** + * @constructor + * + * @param {String} name + */ + initialize: function(name, location) { + OpenLayers.Layer.Marker.prototype.initialize.apply(this, [name]); + this.location = location; + new Ajax.Request(location, + { method: 'get', onComplete:this.parseData.bind(this) } ); + }, + 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 name = ""; var description = ""; + var icon = new OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png',new OpenLayers.Size(10,17)); + 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] == 'locationName') + name = vals[valIndex]; + else if (columns[valIndex] == 'image') + icon.url = vals[valIndex]; + else if (columns[valIndex] == 'title') + location.title = vals[valIndex]; + else if (columns[valIndex] == 'description') + location.description = vals[valIndex]; + } + } + if (set) { + this.addMarker(new OpenLayers.Marker(icon, location)); + } + } + } + } + } +}); + + diff --git a/markers.html b/markers.html index 778b525812..afd7a8dc96 100644 --- a/markers.html +++ b/markers.html @@ -22,6 +22,8 @@ map.addLayer(layer); var markers = new OpenLayers.Layer.Marker( "Markers" ); map.addLayer(markers); + var newl = new OpenLayers.Layer.Text( "text", "./textfile.txt" ); + map.addLayer(newl); var icon = new OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png',new OpenLayers.Size(10,17)); markers.addMarker(new OpenLayers.Marker(icon, new OpenLayers.LonLat(0,0))); markers.addMarker(new OpenLayers.Marker(icon, new OpenLayers.LonLat(-5,-10))); diff --git a/textfile.txt b/textfile.txt new file mode 100644 index 0000000000..f00e5f7355 --- /dev/null +++ b/textfile.txt @@ -0,0 +1,4 @@ +point image +10,20 http://boston.openguides.org/markers/ORANGE.png +2,4 http://boston.openguides.org/markers/AQUA.png +42,-71 http://boston.openguides.org/markers/PURPLE.png