[OpenLayers-Users] How to get position of feature from
DrawFeature?
Arnd Wippermann
arnd.wippermann at web.de
Sat Apr 10 07:50:07 EDT 2010
Hello,
It seems, that there is no place, where one can retrieve the radius and
center of the regular polygon. As a workaround this could be a (dirty)
solution:
Overwite of
OpenLayers.Handler.RegularPolygon.prototype.createGeometry = function()
{
this.angle = Math.PI * ((1/this.sides) - (1/2));
if(this.snapAngle) {
this.angle += this.snapAngle * (Math.PI / 180);
}
this.feature.geometry =
OpenLayers.Geometry.Polygon.createRegularPolygon(
this.origin, this.radius, this.sides, this.snapAngle
);
/* doesn't work. wahrscheinlich wird nur die Geometrie zurückgegeben und
das Feature daraus erstellt.
this.feature.origin = this.origin;
this.feature.radius = this.radius;
*/
this.feature.geometry.origin = this.origin;
this.feature.geometry.radius = this.radius;
//console.log("create:" + this.origin + ", radius: " + this.radius);
};
OpenLayers.Handler.RegularPolygon.prototype.modifyGeometry = function()
{
//console.log("modify:" + this.origin + ", radius: " + this.radius);
var angle, dx, dy, point;
var ring = this.feature.geometry.components[0];
// if the number of sides ever changes, create a new geometry
if(ring.components.length != (this.sides + 1)) {
this.createGeometry();
ring = this.feature.geometry.components[0];
}
for(var i=0; i<this.sides; ++i) {
point = ring.components[i];
angle = this.angle + (i * 2 * Math.PI / this.sides);
point.x = this.origin.x + (this.radius * Math.cos(angle));
point.y = this.origin.y + (this.radius * Math.sin(angle));
point.clearBounds();
}
/* doesn't work. wahrscheinlich wird nur die Geometrie zurückgegeben und
das Feature daraus erstellt.
this.feature.origin = this.origin;
this.feature.radius = this.radius;
*/
this.feature.geometry.origin = this.origin;
this.feature.geometry.radius = this.radius;
};
The radius and center will added to the features geometry object (certainly
bad programming style) and it is now a part of the feature.
alert(yourLayer.features[0].geometry.origin + ", " +
yourLayer.features[0].geometry.radius);
Arnd
-----Ursprüngliche Nachricht-----
Von: users-bounces at openlayers.org [mailto:users-bounces at openlayers.org] Im
Auftrag von gtg287y
Gesendet: Mittwoch, 7. April 2010 20:39
An: users at openlayers.org
Betreff: [OpenLayers-Users] How to get position of feature from DrawFeature?
I am using the Draw Feature Control to draw polygons (namely circles) on a
Vector Layer. I want to be able to save the center of the circle to a
variable, as well as the radius of the circle.
I'm not seeing any native way to do this. When I draw a feature, I'm
assuming it's a Vector Feature, which has "geometry" and "attributes". When
I look at the geometry, I get a bunch of points (I'm assuming 40, since
that's how many sides I told my polygon to have).
My choices are to figure out some complicated way to turn that array of
points into a center and a radius, or (my preference) to manually save the
position the mouse is clicked down, and the position the mouse is let up
(down is center, the distance from up and down is the radius). However, I'm
having difficulties doing that as well: There doesn't seem to be any way to
get a "mousedown" even from a Vector Layer? At least, I'm not seeing any
response when I do things like:
polygonLayer.events.register("mousedown", feature,
function(evt){
console.log("Mouse Down");
});
Which implies to me that Vector Layer doesn't inherit from anythign with a
mouse down?
Doing :
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=function(event) {
alert(event.type) // "mousedown"
}
works fine...everywhere but my map and layer...which doesn't help me much,
and (I'm unfamiliar with javascript still),
polygonLayer.captureEvents(Event.MOUSEDOWN) doesn't work either (firebug
says the function "captureEvents" doesn't exist for VectorLayer.
I must be missing something obvious due to my inexperience: How can I find
out where a user's mouse is when the click down, then let up?
-Jenny
p.s. (Still haven't figured out my polygon refresh issue)
--
View this message in context:
http://n2.nabble.com/How-to-get-position-of-feature-from-DrawFeature-tp48667
10p4866710.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
_______________________________________________
Users mailing list
Users at openlayers.org
http://openlayers.org/mailman/listinfo/users
More information about the Users
mailing list