Is there a way to disable rotating when drawing a Square? Basically make it act like Rectangle mode does, but enforce equal sides.
I'm looking for the new shape equivalent of:
Map1.EditOverlay.EditSettings.IsRotatable = false;
Is there a way to disable rotating when drawing a Square? Basically make it act like Rectangle mode does, but enforce equal sides.
I'm looking for the new shape equivalent of:
Map1.EditOverlay.EditSettings.IsRotatable = false;
Rob,
The drawing operation is hard coded in OpenLayers. But we have a work around for you which is the snap angle. Please paste the following JavaScript to the header tag in our installed sample at samples\MapShapes\DrawEditShapes.aspx.var OnMapCreating = function(map) {
Class.Extent(ThinkGeo.prototype, {
SetDrawMode: function(drawMode) {
eval('var parser = parser' + this.clientId);
if (!parser.editOverlay) {
parser.initDrawControls();
}
var map = parser.map;
if (drawMode == 'Normal') {
for (var key in parser.paintControls) {
if (key != 'Normal') {
parser.paintControls[key].deactivate();
}
}
} else {
for (var key in parser.paintControls) {
if (key == drawMode && drawMode != 'Normal') {
var control = map.getControl(key);
if (drawMode == 'Square') {
control.handler.setOptions({ snapAngle: 45, irregular: false });
}
control.activate();
}
else if (key != 'Normal') {
var control = map.getControl(key);
control.deactivate();
}
}
}
}
});
}
It snaps some specific angle and you still can draw a square which is not rotated. Please have a try and let me know how it works.
Thanks,
Howard
That gets close, but the initial snap is at an angle I don’t want to allow. I want it to snap at 90 degrees, but starting at 45 degrees from the positive x-axis. Any other work arounds?
Background:
The reason I want to limit it like this, is the user is defining an area to export to a same width and height image file.
Rob,
I see, I changed my code; in this way, you can use draw rectangle mode to draw the square you want. Please replace the JavaScript below.
var OnMapCreating = function(map) {
OpenLayers.Handler.RegularPolygon = OpenLayers.Class(OpenLayers.Handler.RegularPolygon, {
move: function(evt) {
var maploc = this.map.getLonLatFromPixel(evt.xy);
var point = new OpenLayers.Geometry.Point(maploc.lon, maploc.lat);
if (this.irregular) {
var ry = Math.sqrt(2) * Math.abs(point.y - this.origin.y) / 2;
this.radius = Math.max(this.map.getResolution() / 2, ry);
} else if (this.fixedRadius) {
this.origin = point;
} else {
this.calculateAngle(point, evt);
this.radius = Math.max(this.map.getResolution() / 2,
point.distanceTo(this.origin));
}
this.modifyGeometry();
if (this.irregular) {
var dx = point.x - this.origin.x;
var dy = point.y - this.origin.y;
var ratio;
if (dy == 0) {
ratio = dx / (this.radius * Math.sqrt(2));
} else {
ratio = dx / dy;
}
this.feature.geometry.resize(1, this.origin, ratio);
this.feature.geometry.move(dx / 2, dy / 2);
}
if (this.sides == 4 && this.irregular) {
var bounds = this.feature.geometry.getBounds();
var width = bounds.getWidth();
bounds.right = bounds.left + width;
bounds.bottom = bounds.top - width;
this.feature = new OpenLayers.Feature.Vector(bounds.toGeometry());
this.clear();
}
this.layer.drawFeature(this.feature, this.style);
}
});
}
Thanks,
Howard
Perfect! Thanks for hacking up OpenLayers for me.
Rob,
You are welcome. We just want to do our best to provide you good services.
Please let me know if you have more questions.
Thanks,
Howard