Skip to content

Instantly share code, notes, and snippets.

@kingofnull
Last active August 15, 2019 15:12
Show Gist options
  • Save kingofnull/0ad644be69d8dd43c05721022ca5c3a5 to your computer and use it in GitHub Desktop.
Save kingofnull/0ad644be69d8dd43c05721022ca5c3a5 to your computer and use it in GitHub Desktop.
drag interaction for openlayers 4 (ol4)
/**
* @constructor
* @extends {ol.interaction.Pointer}
*/
ol.interaction.Drag = function() {
ol.interaction.Pointer.call(this, {
handleDownEvent: ol.interaction.Drag.prototype.handleDownEvent,
handleDragEvent: ol.interaction.Drag.prototype.handleDragEvent,
handleMoveEvent: ol.interaction.Drag.prototype.handleMoveEvent,
handleUpEvent: ol.interaction.Drag.prototype.handleUpEvent
});
/**
* @type {ol.Pixel}
* @private
*/
this.coordinate_ = null;
/**
* @type {string|undefined}
* @private
*/
this.cursor_ = 'move';
/**
* @type {ol.Feature}
* @private
*/
this.feature_ = null;
/**
* @type {string|undefined}
* @private
*/
this.previousCursor_ = undefined;
};
ol.inherits(ol.interaction.Drag, ol.interaction.Pointer);
/**
* @param {ol.MapBrowserEvent} evt Map browser event.
* @return {boolean} `true` to start the drag sequence.
*/
ol.interaction.Drag.prototype.handleDownEvent = function(evt) {
var map = evt.map;
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature) {
return feature;
});
if (feature) {
this.coordinate_ = evt.coordinate;
this.feature_ = feature;
}
return !!feature;
};
/**
* @param {ol.MapBrowserEvent} evt Map browser event.
*/
ol.interaction.Drag.prototype.handleDragEvent = function(evt) {
var deltaX = evt.coordinate[0] - this.coordinate_[0];
var deltaY = evt.coordinate[1] - this.coordinate_[1];
var geometry = this.feature_.getGeometry();
geometry.translate(deltaX, deltaY);
this.coordinate_[0] = evt.coordinate[0];
this.coordinate_[1] = evt.coordinate[1];
};
/**
* @param {ol.MapBrowserEvent} evt Event.
*/
ol.interaction.Drag.prototype.handleMoveEvent = function(evt) {
if (this.cursor_) {
var map = evt.map;
var feature = map.forEachFeatureAtPixel(evt.pixel,
function(feature) {
return feature;
});
var element = evt.map.getTargetElement();
if (feature) {
if (element.style.cursor != this.cursor_) {
this.previousCursor_ = element.style.cursor;
element.style.cursor = this.cursor_;
}
} else if (this.previousCursor_ !== undefined) {
element.style.cursor = this.previousCursor_;
this.previousCursor_ = undefined;
}
}
};
/**
* @return {boolean} `false` to stop the drag sequence.
*/
ol.interaction.Drag.prototype.handleUpEvent = function() {
this.coordinate_ = null;
this.feature_ = null;
return false;
};
@Greed2kk
Copy link

very good and very well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment