Separating out method to get pixel offset
This commit is contained in:
@@ -36,6 +36,12 @@ ol.MapBrowserEvent = function(type, map, browserEvent) {
|
|||||||
*/
|
*/
|
||||||
this.coordinate_ = undefined;
|
this.coordinate_ = undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {ol.Pixel}
|
||||||
|
*/
|
||||||
|
this.pixel_ = null;
|
||||||
|
|
||||||
};
|
};
|
||||||
goog.inherits(ol.MapBrowserEvent, ol.MapEvent);
|
goog.inherits(ol.MapBrowserEvent, ol.MapEvent);
|
||||||
|
|
||||||
@@ -47,15 +53,27 @@ ol.MapBrowserEvent.prototype.getCoordinate = function() {
|
|||||||
if (goog.isDef(this.coordinate_)) {
|
if (goog.isDef(this.coordinate_)) {
|
||||||
return this.coordinate_;
|
return this.coordinate_;
|
||||||
} else {
|
} else {
|
||||||
|
var pixel = this.getPixel();
|
||||||
|
var coordinate = this.map.getCoordinateFromPixel(pixel);
|
||||||
|
this.coordinate_ = coordinate;
|
||||||
|
return coordinate;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get pixel offset of the event from the top-left corner of the map viewport.
|
||||||
|
* @return {ol.Pixel} Pixel offset.
|
||||||
|
*/
|
||||||
|
ol.MapBrowserEvent.prototype.getPixel = function() {
|
||||||
|
if (goog.isNull(this.pixel_)) {
|
||||||
var map = this.map;
|
var map = this.map;
|
||||||
var browserEvent = this.browserEvent;
|
var browserEvent = this.browserEvent;
|
||||||
var eventPosition = goog.style.getRelativePosition(
|
var eventPosition = goog.style.getRelativePosition(
|
||||||
browserEvent, map.getViewport());
|
browserEvent, map.getViewport());
|
||||||
var pixel = new ol.Pixel(eventPosition.x, eventPosition.y);
|
this.pixel_ = new ol.Pixel(eventPosition.x, eventPosition.y);
|
||||||
var coordinate = map.getCoordinateFromPixel(pixel);
|
|
||||||
this.coordinate_ = coordinate;
|
|
||||||
return coordinate;
|
|
||||||
}
|
}
|
||||||
|
return this.pixel_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user