| | 68 | * APIProperty: onCancel |
|---|
| | 69 | * {Function} Define this function if you want to know when a feature's dragging has been |
|---|
| | 70 | * canceled, for example when the feature has been dragged within its tolerance level. |
|---|
| | 71 | * The function should expect to receive two arguments: |
|---|
| | 72 | * the feature that is being dragged and the pixel location of the |
|---|
| | 73 | * mouse. |
|---|
| | 74 | * |
|---|
| | 75 | * Parameters: |
|---|
| | 76 | * feature - {<OpenLayers.Feature.Vector>} The feature that was dragged. |
|---|
| | 77 | * pixel - {<OpenLayers.Pixel>} The pixel location of the mouse. |
|---|
| | 78 | */ |
|---|
| | 79 | onCancel: function(feature, pixel) {}, |
|---|
| | 80 | |
|---|
| | 81 | /** |
|---|
| | 124 | * Property: startPixel |
|---|
| | 125 | * {<OpenLayers.Pixel>} |
|---|
| | 126 | */ |
|---|
| | 127 | startPixel: null, |
|---|
| | 128 | |
|---|
| | 129 | /** |
|---|
| | 130 | * Property: pixelTolerance |
|---|
| | 131 | * {Number} |
|---|
| | 132 | * |
|---|
| | 133 | * Radius (in pixels) from the feature's original position where a drag |
|---|
| | 134 | * should be interpreted as "accidental." This value will be ignored if |
|---|
| | 135 | * the time it took to drag is greater than the timeTolerance below. |
|---|
| | 136 | */ |
|---|
| | 137 | pixelTolerance : 0, |
|---|
| | 138 | |
|---|
| | 139 | /** |
|---|
| | 140 | * Property: timeTolerance |
|---|
| | 141 | * {Number} |
|---|
| | 142 | * |
|---|
| | 143 | * Number of milliseconds in which a drag will be interpreted as |
|---|
| | 144 | * "accidental." If the user takes less time to drag the feature than |
|---|
| | 145 | * given in the timeTolerance, the feature will return to its original |
|---|
| | 146 | * position. |
|---|
| | 147 | */ |
|---|
| | 148 | timeTolerance: 400, |
|---|
| | 149 | |
|---|
| | 150 | /** |
|---|
| | 151 | * Property: dragStart |
|---|
| | 152 | * {Number} |
|---|
| | 153 | * |
|---|
| | 154 | * The time, in milliseconds, in which the drag was started. |
|---|
| | 155 | */ |
|---|
| | 156 | dragStart: null, |
|---|
| | 157 | |
|---|
| | 158 | /** |
|---|
| | 159 | * Property: dragOffset |
|---|
| | 160 | * {<OpenLayers.Pixel>} |
|---|
| | 161 | * |
|---|
| | 162 | * The offset from the feature's actual center lonlat that it was dragged. |
|---|
| | 163 | * This causes the mouse to stay in the same position relative to the feautre's |
|---|
| | 164 | * image while being dragged. |
|---|
| | 165 | */ |
|---|
| | 166 | dragOffset : null, |
|---|
| | 167 | |
|---|
| | 168 | /** |
|---|
| 223 | | var res = this.map.getResolution(); |
|---|
| 224 | | this.feature.geometry.move(res * (pixel.x - this.lastPixel.x), |
|---|
| 225 | | res * (this.lastPixel.y - pixel.y)); |
|---|
| 226 | | this.layer.drawFeature(this.feature); |
|---|
| | 291 | |
|---|
| | 292 | if (!this.dragging) { |
|---|
| | 293 | this.layer.bringToFront(this.feature); |
|---|
| | 294 | this.onStart(this.feature, this.startPixel); |
|---|
| | 295 | this.dragging = true; |
|---|
| | 296 | } |
|---|
| | 297 | |
|---|
| | 298 | // Translate where the feature should move based on the |
|---|
| | 299 | // place in which the user clicked on the feature's image. |
|---|
| | 300 | pixel.x += this.dragOffset.x; |
|---|
| | 301 | pixel.y += this.dragOffset.y; |
|---|
| | 302 | |
|---|
| | 303 | this.feature.move(pixel); |
|---|
| 257 | | this.onComplete(this.feature, pixel); |
|---|
| | 334 | |
|---|
| | 335 | // Check tolerance. |
|---|
| | 336 | var passesTimeTolerance = |
|---|
| | 337 | (new Date()).getTime() > this.dragStart + this.timeTolerance; |
|---|
| | 338 | |
|---|
| | 339 | var xDiff = this.startPixel.x - pixel.x; |
|---|
| | 340 | var yDiff = this.startPixel.y - pixel.y; |
|---|
| | 341 | |
|---|
| | 342 | var passesPixelTolerance = |
|---|
| | 343 | Math.sqrt(Math.pow(xDiff,2) + Math.pow(yDiff,2)) > this.pixelTolerance; |
|---|
| | 344 | |
|---|
| | 345 | if(passesTimeTolerance || passesPixelTolerance){ |
|---|
| | 346 | this.onComplete(this.feature, pixel); |
|---|
| | 347 | } else { |
|---|
| | 348 | var feature = this.feature; |
|---|
| | 349 | var res = this.map.getResolution(); |
|---|
| | 350 | this.feature.geometry.move(res * (this.startPixel.x - pixel.x), |
|---|
| | 351 | res * (pixel.y - this.startPixel.y)); |
|---|
| | 352 | this.layer.drawFeature(this.feature); |
|---|
| | 353 | this.onCancel(feature,pixel); |
|---|
| | 354 | } |
|---|
| | 355 | |
|---|
| | 356 | this.dragging = false; |
|---|