From 4d0f98f4fe0b886869917a7770cdba81afb95b84 Mon Sep 17 00:00:00 2001 From: crschmidt Date: Wed, 17 May 2006 17:21:57 +0000 Subject: [PATCH] Add working code for Markers -- marker layer and marker object. Also add markers.html to demo use of markers functionality. git-svn-id: http://svn.openlayers.org/trunk/openlayers@107 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Layer/Marker.js | 24 ++++++++++++++------- lib/OpenLayers/Marker.js | 11 ++++++++-- markers.html | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 markers.html diff --git a/lib/OpenLayers/Layer/Marker.js b/lib/OpenLayers/Layer/Marker.js index 28701c88e3..b4d6279b42 100644 --- a/lib/OpenLayers/Layer/Marker.js +++ b/lib/OpenLayers/Layer/Marker.js @@ -3,25 +3,35 @@ OpenLayers.Layer.Marker.prototype = Object.extend( new OpenLayers.Layer(), { // markers: store internal marker list - markers:null, + markers: [], initialize: function(name) { OpenLayers.Layer.prototype.initialize.apply(this, arguments); }, // Implement this. It may not need to do anything usually. - moveTo:function(bounds,zoomChanged) { - + moveTo: function(bounds,zoomChanged) { + if (zoomChanged) { + this.div.innerHTML=""; + for(i=0; i < this.markers.length; i++) { + this.drawMarker(this.markers[i]); + } + } }, - addMarker:function(marker) { + addMarker: function(marker) { this.markers.append(marker); + if (this.map && this.map.getExtent()) { + this.drawMarker(marker); + } + }, + drawMarker: function(marker) { var resolution = this.map.getResolution(); var extent = this.map.getExtent(); var pixel = new OpenLayers.Pixel( - resolution * (this.lonlat.lon - extent.minlon), - resolution * (extent.maxlat - this.lonlat.lat) + 1/resolution * (marker.lonlat.lon - extent.minlon), + 1/resolution * (extent.maxlat - marker.lonlat.lat) ); var m = marker.generateMarker(pixel); this.div.appendChild(m); - }, + } }); diff --git a/lib/OpenLayers/Marker.js b/lib/OpenLayers/Marker.js index 804ed16f2c..898ed7c517 100644 --- a/lib/OpenLayers/Marker.js +++ b/lib/OpenLayers/Marker.js @@ -28,8 +28,15 @@ OpenLayers.Marker.prototype = { this.lonlat = lonlat; }, generateMarker: function(pixel) { - var markerObject; - // Create a div here, and set the location to the pixel above + // Create a div here, and set the location to the pixel above modified + // by the icon size. + var markerObject = OpenLayers.Util.createImage( + this.icon.url, + this.icon.size, + new OpenLayers.Pixel( + pixel.x-(this.icon.size.w/2), + pixel.y-this.icon.size.h) + ); return markerObject; } } diff --git a/markers.html b/markers.html new file mode 100644 index 0000000000..778b525812 --- /dev/null +++ b/markers.html @@ -0,0 +1,38 @@ + + + + + + + + +

OpenLayers Example

+
+ +