208 lines
5.0 KiB
JavaScript
208 lines
5.0 KiB
JavaScript
// FIXME should listen on appropriate pane, once it is defined
|
|
// FIXME works for View2D only
|
|
|
|
goog.provide('ol.control.MousePosition');
|
|
|
|
goog.require('goog.array');
|
|
goog.require('goog.dom');
|
|
goog.require('goog.dom.TagName');
|
|
goog.require('goog.events');
|
|
goog.require('goog.events.EventType');
|
|
goog.require('goog.style');
|
|
goog.require('ol.CoordinateFormatType');
|
|
goog.require('ol.Pixel');
|
|
goog.require('ol.Projection');
|
|
goog.require('ol.TransformFunction');
|
|
goog.require('ol.control.Control');
|
|
goog.require('ol.proj');
|
|
|
|
|
|
|
|
/**
|
|
* Create a new control to show the position of the mouse in the map's
|
|
* projection (or any other supplied projection). By default the control is
|
|
* shown in the top right corner of the map but this can be changed by using
|
|
* a css selector .ol-mouse-position.
|
|
*
|
|
* Example:
|
|
*
|
|
* var map = new ol.Map({
|
|
* controls: ol.control.defaults({}, [
|
|
* new ol.control.MousePosition({projection: ol.proj.get('EPSG:4326')})
|
|
* ]),
|
|
* ...
|
|
*
|
|
* @constructor
|
|
* @extends {ol.control.Control}
|
|
* @param {ol.control.MousePositionOptions=} opt_options Mouse position options.
|
|
*/
|
|
ol.control.MousePosition = function(opt_options) {
|
|
|
|
var options = goog.isDef(opt_options) ? opt_options : {};
|
|
|
|
var className = goog.isDef(options.className) ?
|
|
options.className : 'ol-mouse-position';
|
|
|
|
var element = goog.dom.createDom(goog.dom.TagName.DIV, {
|
|
'class': className
|
|
});
|
|
|
|
goog.base(this, {
|
|
element: element,
|
|
map: options.map,
|
|
target: options.target
|
|
});
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.Projection}
|
|
*/
|
|
this.projection_ = ol.proj.get(options.projection);
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.CoordinateFormatType|undefined}
|
|
*/
|
|
this.coordinateFormat_ = options.coordinateFormat;
|
|
|
|
/**
|
|
* @private
|
|
* @type {string}
|
|
*/
|
|
this.undefinedHTML_ = goog.isDef(options.undefinedHTML) ?
|
|
options.undefinedHTML : '';
|
|
|
|
/**
|
|
* @private
|
|
* @type {string}
|
|
*/
|
|
this.renderedHTML_ = element.innerHTML;
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.Projection}
|
|
*/
|
|
this.mapProjection_ = null;
|
|
|
|
/**
|
|
* @private
|
|
* @type {?ol.TransformFunction}
|
|
*/
|
|
this.transform_ = null;
|
|
|
|
/**
|
|
* @private
|
|
* @type {ol.Pixel}
|
|
*/
|
|
this.lastMouseMovePixel_ = null;
|
|
|
|
};
|
|
goog.inherits(ol.control.MousePosition, ol.control.Control);
|
|
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
ol.control.MousePosition.prototype.handleMapPostrender = function(mapEvent) {
|
|
var frameState = mapEvent.frameState;
|
|
if (goog.isNull(frameState)) {
|
|
this.mapProjection_ = null;
|
|
} else {
|
|
if (this.mapProjection_ != frameState.view2DState.projection) {
|
|
this.mapProjection_ = frameState.view2DState.projection;
|
|
this.transform_ = null;
|
|
}
|
|
}
|
|
this.updateHTML_(this.lastMouseMovePixel_);
|
|
};
|
|
|
|
|
|
/**
|
|
* @return {ol.Projection} projection.
|
|
*/
|
|
ol.control.MousePosition.prototype.getProjection = function() {
|
|
return this.projection_;
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {goog.events.BrowserEvent} browserEvent Browser event.
|
|
* @protected
|
|
*/
|
|
ol.control.MousePosition.prototype.handleMouseMove = function(browserEvent) {
|
|
var map = this.getMap();
|
|
var eventPosition = goog.style.getRelativePosition(
|
|
browserEvent, map.getViewport());
|
|
this.lastMouseMovePixel_ = [eventPosition.x, eventPosition.y];
|
|
this.updateHTML_(this.lastMouseMovePixel_);
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {goog.events.BrowserEvent} browserEvent Browser event.
|
|
* @protected
|
|
*/
|
|
ol.control.MousePosition.prototype.handleMouseOut = function(browserEvent) {
|
|
this.updateHTML_(null);
|
|
this.lastMouseMovePixel_ = null;
|
|
};
|
|
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
ol.control.MousePosition.prototype.setMap = function(map) {
|
|
goog.base(this, 'setMap', map);
|
|
if (!goog.isNull(map)) {
|
|
var viewport = map.getViewport();
|
|
this.listenerKeys.push(
|
|
goog.events.listen(viewport, goog.events.EventType.MOUSEMOVE,
|
|
this.handleMouseMove, false, this),
|
|
goog.events.listen(viewport, goog.events.EventType.MOUSEOUT,
|
|
this.handleMouseOut, false, this)
|
|
);
|
|
}
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {ol.ProjectionLike} projection Projection.
|
|
*/
|
|
ol.control.MousePosition.prototype.setProjection = function(projection) {
|
|
this.projection_ = ol.proj.get(projection);
|
|
this.transform_ = null;
|
|
};
|
|
|
|
|
|
/**
|
|
* @param {?ol.Pixel} pixel Pixel.
|
|
* @private
|
|
*/
|
|
ol.control.MousePosition.prototype.updateHTML_ = function(pixel) {
|
|
var html = this.undefinedHTML_;
|
|
if (!goog.isNull(pixel)) {
|
|
if (goog.isNull(this.transform_)) {
|
|
if (!goog.isNull(this.projection_)) {
|
|
this.transform_ = ol.proj.getTransformFromProjections(
|
|
this.mapProjection_, this.projection_);
|
|
} else {
|
|
this.transform_ = ol.proj.identityTransform;
|
|
}
|
|
}
|
|
var map = this.getMap();
|
|
var coordinate = map.getCoordinateFromPixel(pixel);
|
|
if (!goog.isNull(coordinate)) {
|
|
this.transform_(coordinate, coordinate);
|
|
if (goog.isDef(this.coordinateFormat_)) {
|
|
html = this.coordinateFormat_(coordinate);
|
|
} else {
|
|
html = coordinate.toString();
|
|
}
|
|
}
|
|
}
|
|
if (!goog.isDef(this.renderedHTML_) || html != this.renderedHTML_) {
|
|
this.element.innerHTML = html;
|
|
this.renderedHTML_ = html;
|
|
}
|
|
};
|