Add a drag-feature example
This example also demonstrates how to implement a custom interaction that inherits from ol.interaction.Pointer.
This commit is contained in:
51
examples/drag-features.html
Normal file
51
examples/drag-features.html
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="chrome=1">
|
||||||
|
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
|
||||||
|
<link rel="stylesheet" href="../css/ol.css" type="text/css">
|
||||||
|
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
|
||||||
|
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
|
||||||
|
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
|
||||||
|
<title>Drag features example</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||||
|
<div class="navbar-inner">
|
||||||
|
<div class="container">
|
||||||
|
<a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="container-fluid">
|
||||||
|
|
||||||
|
<div class="row-fluid">
|
||||||
|
<div class="span12">
|
||||||
|
<div id="map" class="map"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row-fluid">
|
||||||
|
|
||||||
|
<div class="span12">
|
||||||
|
<h4 id="title">Drag features example</h4>
|
||||||
|
<p id="shortdesc">Example of a drag features interaction.</p>
|
||||||
|
<div id="docs">
|
||||||
|
<p>See the <a href="drag-features.js" target="_blank">drag-features.js source</a> to see how this is done.</p>
|
||||||
|
</div>
|
||||||
|
<div id="tags">drag, feature, vector, editing</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="../resources/jquery.min.js" type="text/javascript"></script>
|
||||||
|
<script src="../resources/example-behaviour.js" type="text/javascript"></script>
|
||||||
|
<script src="loader.js?id=drag-features" type="text/javascript"></script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
192
examples/drag-features.js
Normal file
192
examples/drag-features.js
Normal file
@@ -0,0 +1,192 @@
|
|||||||
|
goog.require('ol.Feature');
|
||||||
|
goog.require('ol.Map');
|
||||||
|
goog.require('ol.View');
|
||||||
|
goog.require('ol.geom.LineString');
|
||||||
|
goog.require('ol.geom.Point');
|
||||||
|
goog.require('ol.geom.Polygon');
|
||||||
|
goog.require('ol.interaction');
|
||||||
|
goog.require('ol.interaction.Pointer');
|
||||||
|
goog.require('ol.layer.Tile');
|
||||||
|
goog.require('ol.layer.Vector');
|
||||||
|
goog.require('ol.source.TileJSON');
|
||||||
|
goog.require('ol.source.Vector');
|
||||||
|
goog.require('ol.style.Fill');
|
||||||
|
goog.require('ol.style.Icon');
|
||||||
|
goog.require('ol.style.Stroke');
|
||||||
|
goog.require('ol.style.Style');
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Define a namespace for the application.
|
||||||
|
*/
|
||||||
|
window.app = {};
|
||||||
|
var app = window.app;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
* @extends {ol.interaction.Pointer}
|
||||||
|
*/
|
||||||
|
app.Drag = function() {
|
||||||
|
|
||||||
|
ol.interaction.Pointer.call(this, {
|
||||||
|
handleDownEvent: app.Drag.prototype.handleDownEvent,
|
||||||
|
handleDragEvent: app.Drag.prototype.handleDragEvent,
|
||||||
|
handleMoveEvent: app.Drag.prototype.handleMoveEvent,
|
||||||
|
handleUpEvent: app.Drag.prototype.handleUpEvent
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {ol.Pixel}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.coordinate_ = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {string|undefined}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.cursor_ = 'pointer';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {ol.Feature}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.feature_ = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @type {string|undefined}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
this.previousCursor_ = undefined;
|
||||||
|
|
||||||
|
};
|
||||||
|
ol.inherits(app.Drag, ol.interaction.Pointer);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.MapBrowserEvent} evt Map browser event.
|
||||||
|
* @return {boolean} `true` to start the drag sequence.
|
||||||
|
*/
|
||||||
|
app.Drag.prototype.handleDownEvent = function(evt) {
|
||||||
|
var map = evt.map;
|
||||||
|
|
||||||
|
var feature = map.forEachFeatureAtPixel(evt.pixel,
|
||||||
|
function(feature, layer) {
|
||||||
|
return feature;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (feature) {
|
||||||
|
this.coordinate_ = evt.coordinate;
|
||||||
|
this.feature_ = feature;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !!feature;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.MapBrowserEvent} evt Map browser event.
|
||||||
|
*/
|
||||||
|
app.Drag.prototype.handleDragEvent = function(evt) {
|
||||||
|
var map = evt.map;
|
||||||
|
|
||||||
|
var feature = map.forEachFeatureAtPixel(evt.pixel,
|
||||||
|
function(feature, layer) {
|
||||||
|
return feature;
|
||||||
|
});
|
||||||
|
|
||||||
|
var deltaX = evt.coordinate[0] - this.coordinate_[0];
|
||||||
|
var deltaY = evt.coordinate[1] - this.coordinate_[1];
|
||||||
|
|
||||||
|
var geometry = /** @type {ol.geom.SimpleGeometry} */
|
||||||
|
(this.feature_.getGeometry());
|
||||||
|
geometry.translate(deltaX, deltaY);
|
||||||
|
|
||||||
|
this.coordinate_[0] = evt.coordinate[0];
|
||||||
|
this.coordinate_[1] = evt.coordinate[1];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.MapBrowserEvent} evt Event.
|
||||||
|
*/
|
||||||
|
app.Drag.prototype.handleMoveEvent = function(evt) {
|
||||||
|
if (this.cursor_) {
|
||||||
|
var map = evt.map;
|
||||||
|
var feature = map.forEachFeatureAtPixel(evt.pixel,
|
||||||
|
function(feature, layer) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ol.MapBrowserEvent} evt Map browser event.
|
||||||
|
* @return {boolean} `false` to stop the drag sequence.
|
||||||
|
*/
|
||||||
|
app.Drag.prototype.handleUpEvent = function(evt) {
|
||||||
|
this.coordinate_ = null;
|
||||||
|
this.feature_ = null;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
var pointFeature = new ol.Feature(new ol.geom.Point([0, 0]));
|
||||||
|
|
||||||
|
var lineFeature = new ol.Feature(
|
||||||
|
new ol.geom.LineString([[-1e7, 1e6], [-1e6, 3e6]]));
|
||||||
|
|
||||||
|
var polygonFeature = new ol.Feature(
|
||||||
|
new ol.geom.Polygon([[[-3e6, -1e6], [-3e6, 1e6],
|
||||||
|
[-1e6, 1e6], [-1e6, -1e6], [-3e6, -1e6]]]));
|
||||||
|
|
||||||
|
|
||||||
|
var map = new ol.Map({
|
||||||
|
interactions: ol.interaction.defaults().extend([new app.Drag()]),
|
||||||
|
layers: [
|
||||||
|
new ol.layer.Tile({
|
||||||
|
source: new ol.source.TileJSON({
|
||||||
|
url: 'http://api.tiles.mapbox.com/v3/mapbox.geography-class.jsonp'
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
new ol.layer.Vector({
|
||||||
|
source: new ol.source.Vector({
|
||||||
|
features: [pointFeature, lineFeature, polygonFeature]
|
||||||
|
}),
|
||||||
|
style: new ol.style.Style({
|
||||||
|
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
|
||||||
|
anchor: [0.5, 46],
|
||||||
|
anchorXUnits: 'fraction',
|
||||||
|
anchorYUnits: 'pixels',
|
||||||
|
opacity: 0.95,
|
||||||
|
src: 'data/icon.png'
|
||||||
|
})),
|
||||||
|
stroke: new ol.style.Stroke({
|
||||||
|
width: 3,
|
||||||
|
color: [255, 0, 0, 1]
|
||||||
|
}),
|
||||||
|
fill: new ol.style.Fill({
|
||||||
|
color: [0, 0, 255, 0.6]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
],
|
||||||
|
target: 'map',
|
||||||
|
view: new ol.View({
|
||||||
|
center: [0, 0],
|
||||||
|
zoom: 2
|
||||||
|
})
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user