make the drawing handlers create the sketch feature at an appropriate time, when we can actually derive geographic coordinates from a pixel, r=ahocevar (References #3327)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@12046 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2011-06-06 07:12:52 +00:00
parent 9dcdbac238
commit 9b361ef35d
9 changed files with 116 additions and 208 deletions

View File

@@ -102,13 +102,10 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
* feature.
*/
createFeature: function(pixel) {
var geometry;
if(pixel) {
var lonlat = this.map.getLonLatFromPixel(pixel);
geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
} else {
geometry = new OpenLayers.Geometry.Point();
}
var lonlat = this.map.getLonLatFromPixel(pixel);
var geometry = new OpenLayers.Geometry.Point(
lonlat.lon, lonlat.lat
);
this.point = new OpenLayers.Feature.Vector(geometry);
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([this.point.geometry])
@@ -121,9 +118,13 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
/**
* Method: destroyFeature
* Destroy temporary geometries
*
* Parameters:
* force - {Boolean} Destroy even if persist is true.
*/
destroyFeature: function() {
OpenLayers.Handler.Point.prototype.destroyFeature.apply(this);
destroyFeature: function(force) {
OpenLayers.Handler.Point.prototype.destroyFeature.call(
this, force);
this.line = null;
},
@@ -193,6 +194,9 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
* drawing - {Boolean} Indicate if we're currently drawing.
*/
modifyFeature: function(pixel, drawing) {
if(!this.line) {
this.createFeature(pixel);
}
var lonlat = this.control.map.getLonLatFromPixel(pixel);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;

View File

@@ -168,7 +168,6 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
}, this.layerOptions);
this.layer = new OpenLayers.Layer.Vector(this.CLASS_NAME, options);
this.map.addLayer(this.layer);
this.createFeature();
return true;
},
@@ -180,13 +179,10 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* pixel - {<OpenLayers.Pixel>} A pixel location on the map.
*/
createFeature: function(pixel) {
var geometry;
if(pixel) {
var lonlat = this.map.getLonLatFromPixel(pixel);
geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
} else {
geometry = new OpenLayers.Geometry.Point();
}
var lonlat = this.map.getLonLatFromPixel(pixel);
var geometry = new OpenLayers.Geometry.Point(
lonlat.lon, lonlat.lat
);
this.point = new OpenLayers.Feature.Vector(geometry);
this.callback("create", [this.point.geometry, this.point]);
this.point.geometry.clearBounds();
@@ -201,14 +197,14 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
if(!OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) {
return false;
}
this.cancel(true);
this.cancel();
// If a layer's map property is set to null, it means that that layer
// isn't added to the map. Since we ourself added the layer to the map
// in activate(), we can assume that if this.layer.map is null it means
// that the layer has been destroyed (as a result of map.destroy() for
// example.
if (this.layer.map != null) {
this.destroyFeature();
this.destroyFeature(true);
this.layer.destroy(false);
}
this.layer = null;
@@ -219,9 +215,12 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
/**
* Method: destroyFeature
* Destroy the temporary geometries
*
* Parameters:
* force - {Boolean} Destroy even if persist is true.
*/
destroyFeature: function() {
if(this.layer) {
destroyFeature: function(force) {
if(this.layer && (force || !this.persist)) {
this.layer.destroyFeatures();
}
this.point = null;
@@ -243,12 +242,10 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* Finish the geometry and call the "done" callback.
*
* Parameters:
* cancel - {Boolean} Call cancel instead of done callback. Default is
* false.
* noNew - {Boolean} Do not create a new feature after
* finalization. Default is false.
* cancel - {Boolean} Call cancel instead of done callback. Default
* is false.
*/
finalize: function(cancel, noNew) {
finalize: function(cancel) {
var key = cancel ? "cancel" : "done";
this.drawing = false;
this.mouseDown = false;
@@ -256,24 +253,15 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
this.lastUp = null;
this.lastTouchPx = null;
this.callback(key, [this.geometryClone()]);
if(cancel || !this.persist) {
this.destroyFeature();
}
if(!noNew && this.active) {
this.createFeature();
}
this.destroyFeature(cancel);
},
/**
* APIMethod: cancel
* Finish the geometry and call the "cancel" callback.
*
* Parameters:
* noNew - {Boolean} Do not create a new feature after
* cancelation. Default is false.
*/
cancel: function(noNew) {
this.finalize(true, noNew);
cancel: function() {
this.finalize(true);
},
/**
@@ -316,6 +304,9 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* pixel - {<OpenLayers.Pixel>} A pixel location on the map.
*/
modifyFeature: function(pixel) {
if(!this.point) {
this.createFeature(pixel);
}
var lonlat = this.map.getLonLatFromPixel(pixel);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;

View File

@@ -75,13 +75,10 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
* feature.
*/
createFeature: function(pixel) {
var geometry;
if(pixel) {
var lonlat = this.map.getLonLatFromPixel(pixel);
geometry = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat);
} else {
geometry = new OpenLayers.Geometry.Point();
}
var lonlat = this.map.getLonLatFromPixel(pixel);
var geometry = new OpenLayers.Geometry.Point(
lonlat.lon, lonlat.lat
);
this.point = new OpenLayers.Feature.Vector(geometry);
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LinearRing([this.point.geometry])
@@ -251,9 +248,13 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
/**
* Method: destroyFeature
* Destroy temporary geometries
*
* Parameters:
* force - {Boolean} Destroy even if persist is true.
*/
destroyFeature: function() {
OpenLayers.Handler.Path.prototype.destroyFeature.apply(this);
destroyFeature: function(force) {
OpenLayers.Handler.Path.prototype.destroyFeature.call(
this, force);
this.polygon = null;
},