[OpenLayers-Users] ortho snapping

mika at digikartta.net mika at digikartta.net
Mon Dec 19 09:31:27 EST 2011


 Hi,
 I implemented a little extension into snapping control. Could someone 
 please help me to do it a little smarter looking (and functioning) and 
 maybe you could give me some feedback; does this extension make any 
 sense. I don't know how to use OL Class framework. This is how I got 
 this working anyway. Don't tell this kind of feature is already 
 implemented ;-)

 So, if you set snapToOrtho on true, you can only draw vertical and 
 horizontal lines. Still the original snap will override this.

 Or should I ask from developers list?

 - mika -

 Anyway, here you go:

         OpenLayers.Control.SnappingPlus = 
 OpenLayers.Class(OpenLayers.Control.Snapping, {
             snapToOrtho: false,
             setSnapToOrtho: function (ortho) { ortho ? this.snapToOrtho 
 = true : this.snapToOrtho = false },
             CLASS_NAME: "OpenLayers.Control.SnappingPlus"
             });
             OpenLayers.Control.SnappingPlus.prototype.onSketchModified 
 = function (event) {
                 this.feature = event.feature;
                 if (this.snapToOrtho) {
                 var result, target;
                 for (var i = 0, len = this.targets.length; i < len; 
 ++i) {
                     target = this.targets[i];
                     result = this.testTarget(target, event.vertex);
                     if (!result) {
                         var comp = this.feature.geometry.components;
                         var prevpoint;
                         comp.length > 1 ? prevpoint = comp[comp.length 
 - 2] : prevpoint = event.vertex;
                         Math.abs(event.vertex.x - prevpoint.x) > 
 Math.abs(event.vertex.y - prevpoint.y) ? event.vertex.y = prevpoint.y : 
 event.vertex.x = prevpoint.x;
                         break;
                         }
                     }
                 }
             this.considerSnapping(event.vertex, event.vertex);
             }



More information about the Users mailing list