[OpenLayers-Trac] Re: [OpenLayers] #3591: Alter geometry on the fly on feature drawing

OpenLayers trac-20090302 at openlayers.org
Thu Dec 8 05:01:38 EST 2011


#3591: Alter geometry on the fly on feature drawing
----------------------------+-----------------------------------------------
 Reporter:  anrikun         |       Owner:                        
     Type:  feature         |      Status:  new                   
 Priority:  major           |   Milestone:  2.12 Release          
Component:  Feature.Vector  |     Version:  2.11                  
 Keywords:                  |       State:  Awaiting User Feedback
----------------------------+-----------------------------------------------

Comment(by anrikun):

 Thanks for pointing out these links.
 After reviewing them, I don't think that they allow what the patch I have
 submitted does.
 As a concrete exmaple, here is the code of what I am doing implementing
 the new prepareGeometry method. The class below is a vector feature that
 represents an arrow between two vertices. I want the head of the arrow to
 have a fixed size, that is why I must recalculate the coordinates of the
 head each time the feature is redrawn on resolution changes.
 I have simplified the code a bit so that only things that matter here are
 present.


 {{{
 MyNS.Arrow = OpenLayers.Class(OpenLayers.Feature.Vector, {
   points: null, // The 2 end-points of the arrow.
   width: 15, // The width of the head of the arrow.
   initialize: function(from, to, attributes, style) {
     OpenLayers.Feature.Vector.prototype.initialize.apply(this, [new
 OpenLayers.Geometry.LineString(), attributes, style]);
     this.points = [from.clone(), to.clone()];
   },
   getRadiusAngle: function() {
     if (this.points[0] && this.points[1]) {
       var x = this.points[1].x - this.points[0].x, y = this.points[1].y -
 this.points[0].y;
       var r = Math.sqrt(x * x + y * y);
       var rad = (r > 0) ? ((y > 0) ? 1 : -1) * Math.acos(x / r) : 0;
       return {radius: r, angle: 180 * rad / Math.PI};
     }
   },
   prepareGeometry: function() {
     this.geometry.clearBounds();
     this.geometry.components = [];
     var ra = this.getRadiusAngle();
     if (ra && this.layer && this.layer.map) {
       var r = ra.radius;
       var a = Math.min(this.layer.map.getResolution() * this.width / 2,
 r);
       this.geometry.components = [
         new OpenLayers.Geometry.Point(0, 0),
         new OpenLayers.Geometry.Point(r, 0),
         new OpenLayers.Geometry.Point(r - a, -a), // Here we use "a" that
 is computed based on the current map resolution.
         new OpenLayers.Geometry.Point(r - a,  a), // This way, the head of
 the arrow have a fixed size.
         new OpenLayers.Geometry.Point(r, 0)
       ];
       var origin = this.points[0];
       this.geometry.move(origin.x, origin.y);
       this.geometry.rotate(ra.angle, origin);
     }
   },
   CLASS_NAME: 'MyNS.Arrow'
 });


 }}}

-- 
Ticket URL: <http://trac.openlayers.org/ticket/3591#comment:3>
OpenLayers <http://openlayers.org/>
A free AJAX map viewer


More information about the Trac mailing list