Merge pull request #4268 from jonataswalker/get-overlay-by-id
Add method for retrieving ol.Overlay by id
This commit is contained in:
@@ -342,6 +342,13 @@ ol.Map = function(options) {
|
||||
*/
|
||||
this.overlays_ = optionsInternal.overlays;
|
||||
|
||||
/**
|
||||
* A lookup of overlays by id.
|
||||
* @private
|
||||
* @type {Object.<string, ol.Overlay>}
|
||||
*/
|
||||
this.overlayIdIndex_ = {};
|
||||
|
||||
/**
|
||||
* @type {ol.renderer.Map}
|
||||
* @private
|
||||
@@ -460,21 +467,14 @@ ol.Map = function(options) {
|
||||
event.element.setMap(null);
|
||||
}, false, this);
|
||||
|
||||
this.overlays_.forEach(
|
||||
/**
|
||||
* @param {ol.Overlay} overlay Overlay.
|
||||
* @this {ol.Map}
|
||||
*/
|
||||
function(overlay) {
|
||||
overlay.setMap(this);
|
||||
}, this);
|
||||
this.overlays_.forEach(this.addOverlayInternal_, this);
|
||||
|
||||
goog.events.listen(this.overlays_, ol.CollectionEventType.ADD,
|
||||
/**
|
||||
* @param {ol.CollectionEvent} event Collection event.
|
||||
*/
|
||||
function(event) {
|
||||
event.element.setMap(this);
|
||||
this.addOverlayInternal_(/** @type {ol.Overlay} */ (event.element));
|
||||
}, false, this);
|
||||
|
||||
goog.events.listen(this.overlays_, ol.CollectionEventType.REMOVE,
|
||||
@@ -482,6 +482,10 @@ ol.Map = function(options) {
|
||||
* @param {ol.CollectionEvent} event Collection event.
|
||||
*/
|
||||
function(event) {
|
||||
var id = event.element.getId();
|
||||
if (id !== undefined) {
|
||||
delete this.overlayIdIndex_[id.toString()];
|
||||
}
|
||||
event.element.setMap(null);
|
||||
}, false, this);
|
||||
|
||||
@@ -539,6 +543,20 @@ ol.Map.prototype.addOverlay = function(overlay) {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* This deals with map's overlay collection changes.
|
||||
* @param {ol.Overlay} overlay Overlay.
|
||||
* @private
|
||||
*/
|
||||
ol.Map.prototype.addOverlayInternal_ = function(overlay) {
|
||||
var id = overlay.getId();
|
||||
if (id !== undefined) {
|
||||
this.overlayIdIndex_[id.toString()] = overlay;
|
||||
}
|
||||
overlay.setMap(this);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Add functions to be called before rendering. This can be used for attaching
|
||||
* animations before updating the map's view. The {@link ol.animation}
|
||||
@@ -767,6 +785,20 @@ ol.Map.prototype.getOverlays = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get an overlay by its identifier (the value returned by overlay.getId()).
|
||||
* Note that the index treats string and numeric identifiers as the same. So
|
||||
* `map.getOverlayById(2)` will return an overlay with id `'2'` or `2`.
|
||||
* @param {string|number} id Overlay identifier.
|
||||
* @return {ol.Overlay} Overlay.
|
||||
* @api
|
||||
*/
|
||||
ol.Map.prototype.getOverlayById = function(id) {
|
||||
var overlay = this.overlayIdIndex_[id.toString()];
|
||||
return overlay !== undefined ? overlay : null;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the map interactions. Modifying this collection changes the interactions
|
||||
* associated with the map.
|
||||
|
||||
@@ -73,6 +73,12 @@ ol.Overlay = function(options) {
|
||||
|
||||
goog.base(this);
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {number|string|undefined}
|
||||
*/
|
||||
this.id_ = options.id;
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @type {boolean}
|
||||
@@ -187,6 +193,16 @@ ol.Overlay.prototype.getElement = function() {
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the overlay identifier which is set on constructor.
|
||||
* @return {number|string|undefined} Id.
|
||||
* @api
|
||||
*/
|
||||
ol.Overlay.prototype.getId = function() {
|
||||
return this.id_;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Get the map associated with this overlay.
|
||||
* @return {ol.Map|undefined} The map that the overlay is part of.
|
||||
|
||||
Reference in New Issue
Block a user