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