Add method for retrieving ol.Overlay by id

This commit is contained in:
jonataswalker
2015-10-14 12:15:53 -03:00
parent b4061e7b35
commit d29e5eaef1
4 changed files with 291 additions and 3 deletions

View File

@@ -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
@@ -466,7 +473,7 @@ ol.Map = function(options) {
* @this {ol.Map}
*/
function(overlay) {
overlay.setMap(this);
this.addOverlayInternal(overlay);
}, this);
goog.events.listen(this.overlays_, ol.CollectionEventType.ADD,
@@ -474,7 +481,7 @@ ol.Map = function(options) {
* @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,7 +489,7 @@ ol.Map = function(options) {
* @param {ol.CollectionEvent} event Collection event.
*/
function(event) {
event.element.setMap(null);
this.removeOverlayInternal(/** @type {ol.Overlay} */ (event.element));
}, false, this);
};
@@ -539,6 +546,23 @@ ol.Map.prototype.addOverlay = function(overlay) {
};
/**
* This deals with map's overlay collection changes.
* @param {ol.Overlay} overlay Overlay.
* @protected
*/
ol.Map.prototype.addOverlayInternal = function(overlay) {
var id = overlay.getId();
if (id !== undefined) {
this.overlayIdIndex_[id.toString()] = overlay;
}
overlay.setMap(this);
goog.events.listen(
overlay, ol.Object.getChangeEventType(overlay.getOverlayIdProperty()),
this.handleOverlayIdChange_, false, 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 +791,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.
@@ -1065,6 +1103,28 @@ ol.Map.prototype.handleTileChange_ = function() {
};
/**
* @param {goog.events.Event} event Event.
* @private
*/
ol.Map.prototype.handleOverlayIdChange_ = function(event) {
var overlay = /** @type {ol.Overlay} */ (event.target);
var id = overlay.getId().toString();
var oldId = event.oldValue;
if (oldId && oldId != id) {
delete this.overlayIdIndex_[oldId];
}
if (id in this.overlayIdIndex_) {
if (this.overlayIdIndex_[id] !== overlay) {
delete this.overlayIdIndex_[id];
this.overlayIdIndex_[id] = overlay;
}
} else {
this.overlayIdIndex_[id] = overlay;
}
};
/**
* @private
*/
@@ -1245,6 +1305,20 @@ ol.Map.prototype.removeOverlay = function(overlay) {
};
/**
* This deals with map's overlay collection changes.
* @param {ol.Overlay} overlay Overlay.
* @protected
*/
ol.Map.prototype.removeOverlayInternal = function(overlay) {
var id = overlay.getId();
if (id !== undefined) {
delete this.overlayIdIndex_[id.toString()];
}
overlay.setMap(null);
};
/**
* @param {number} time Time.
* @private