Ticket #893: patch-893-A3.diff
| File patch-893-A3.diff, 8.0 kB (added by fredj, 1 year ago) |
|---|
-
tests/Layer/test_Vector.html
old new 158 158 } 159 159 160 160 function test_Layer_Vector_externalGraphic(t) { 161 t.plan( 9);161 t.plan(11); 162 162 // base layer is needed for getResolution() to return a value, 163 163 // otherwise VML test will fail because style.left and style.top 164 164 // cannot be set … … 169 169 format: 'image/png'}); 170 170 171 171 var layer = new OpenLayers.Layer.Vector("Test Layer"); 172 var renderer = layer.renderer; 172 173 var map = new OpenLayers.Map('map'); 173 174 map.addLayers([baseLayer, layer]); 174 175 175 var geometry = new OpenLayers.Geometry.Point(10, 10); 176 var geometryX = 10; 177 var geometryY = 10; 178 var geometry = new OpenLayers.Geometry.Point(geometryX, geometryY); 176 179 var feature = new OpenLayers.Feature.Vector(geometry); 177 180 181 map.zoomToMaxExtent(); 182 178 183 var customStyle1 = new Object({ 179 184 externalGraphic: 'test.png', 180 185 pointRadius: 10 … … 197 202 graphicWidth: 24, 198 203 graphicOpacity: 1 199 204 }); 205 var customStyle6 = new Object({ 206 externalGraphic: 'test.png', 207 graphicWidth: 24, 208 graphicHeight: 16, 209 graphicXOffset: -24, 210 graphicYOffset: -16 211 }); 200 212 201 var root = layer.renderer.root;213 var root = renderer.root; 202 214 if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.SVG') { 203 215 feature.style = customStyle1; 204 216 layer.drawFeature(feature); … … 237 249 t.eq(root.firstChild.getAttributeNS(null, 'style'), 238 250 'opacity: '+customStyle5.graphicOpacity.toString()+';', 239 251 "graphicOpacity correctly set"); 252 feature.style = customStyle6; 253 layer.drawFeature(feature); 254 var x = geometryX / renderer.getResolution() + renderer.left; 255 var y = geometryY / renderer.getResolution() - renderer.top; 256 // SVG setStyle() gets x and y using getAttributeNS(), which returns 257 // a value with only 3 decimal digits. To mimic this we use toFixed(3) here 258 x = x.toFixed(3); 259 y = y.toFixed(3); 260 // toFixed() returns a string 261 x = parseFloat(x); 262 y = parseFloat(y); 263 t.eq(root.firstChild.getAttributeNS(null, 'x'), 264 (x + customStyle6.graphicXOffset).toFixed().toString(), 265 "graphicXOffset correctly set"); 266 t.eq(root.firstChild.getAttributeNS(null, 'y'), 267 (-y + customStyle6.graphicYOffset).toFixed().toString(), 268 "graphicYOffset correctly set"); 240 269 } 241 270 if (layer.renderer.CLASS_NAME == 'OpenLayers.Renderer.VML') { 242 271 feature.style = customStyle1; … … 279 308 t.eq(opacity, 280 309 customStyle5.graphicOpacity, 281 310 "graphicOpacity correctly set"); 311 feature.style = customStyle6; 312 layer.drawFeature(feature); 313 var x = geometryX / renderer.getResolution(); 314 var y = geometryY / renderer.getResolution(); 315 t.eq(root.firstChild.style.left, 316 (x + customStyle6.graphicXOffset).toFixed().toString()+'px', 317 "graphicXOffset correctly set"); 318 319 t.eq(root.firstChild.style.top, 320 (y - (customStyle6.graphicYOffset+parseInt(root.firstChild.style.height))).toFixed().toString()+'px', 321 "graphicYOffset correctly set"); 282 322 283 323 } 284 324 } -
lib/OpenLayers/Feature/Vector.js
old new 263 263 * - graphicWidth, 264 264 * - graphicHeight, 265 265 * - graphicOpacity 266 * - graphicXOffset 267 * - graphicYOffset 266 268 */ 267 269 OpenLayers.Feature.Vector.style = { 268 270 'default': { -
lib/OpenLayers/Renderer/VML.js
old new 181 181 var height = style.graphicHeight || style.graphicWidth; 182 182 width = width ? width : style.pointRadius*2; 183 183 height = height ? height : style.pointRadius*2; 184 184 185 var resolution = this.getResolution(); 185 node.style.left = (geometry.x/resolution-.5*width).toFixed(); 186 node.style.top = (geometry.y/resolution-.5*height).toFixed(); 186 var xOffset = (style.graphicXOffset != undefined) ? 187 style.graphicXOffset : -(0.5 * width); 188 var yOffset = (style.graphicYOffset != undefined) ? 189 style.graphicYOffset : -(0.5 * height); 190 191 node.style.left = ((geometry.x/resolution)+xOffset).toFixed(); 192 node.style.top = ((geometry.y/resolution)-(yOffset+height)).toFixed(); 187 193 node.style.width = width; 188 194 node.style.height = height; 189 195 -
lib/OpenLayers/Renderer/SVG.js
old new 184 184 if (style.externalGraphic) { 185 185 // remove old node 186 186 var id = node.getAttributeNS(null, "id"); 187 var x = node.getAttributeNS(null, "cx");188 var y = node.getAttributeNS(null, "cy");187 var x = parseFloat(node.getAttributeNS(null, "cx")); 188 var y = parseFloat(node.getAttributeNS(null, "cy")); 189 189 var _featureId = node._featureId; 190 190 var _geometryClass = node._geometryClass; 191 191 var _style = node._style; … … 206 206 var height = style.graphicHeight || style.graphicWidth; 207 207 width = width ? width : style.pointRadius*2; 208 208 height = height ? height : style.pointRadius*2; 209 var xOffset = (style.graphicXOffset != undefined) ? 210 style.graphicXOffset : -(0.5 * width); 211 var yOffset = (style.graphicYOffset != undefined) ? 212 style.graphicYOffset : -(0.5 * height); 209 213 var opacity = style.graphicOpacity || style.fillOpacity; 210 214 211 node.setAttributeNS(null, "x", x-(.5*width).toFixed());212 node.setAttributeNS(null, "y", -y-(.5*height).toFixed());215 node.setAttributeNS(null, "x", (x + xOffset).toFixed()); 216 node.setAttributeNS(null, "y", (-y + yOffset).toFixed()); 213 217 node.setAttributeNS(null, "width", width); 214 218 node.setAttributeNS(null, "height", height); 215 219 node.setAttributeNS("http://www.w3.org/1999/xlink", "href", style.externalGraphic); -
examples/vector-features.html
old new 58 58 // of the image will be ignored 59 59 style_mark.graphicWidth = 24; 60 60 style_mark.graphicHeight = 20; 61 style_mark.graphicXOffset = -(style_mark.graphicWidth/2); // this is the default value 62 style_mark.graphicYOffset = -style_mark.graphicHeight; 61 63 style_mark.externalGraphic = "../img/marker.png"; 62 64 63 65 var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {style: layer_style});
