diff --git a/src/leaflet/icon/GenericIcon.ts b/src/leaflet/icon/GenericIcon.ts index 40f71e2..4603b35 100644 --- a/src/leaflet/icon/GenericIcon.ts +++ b/src/leaflet/icon/GenericIcon.ts @@ -117,6 +117,16 @@ export class GenericIcon extends DivIcon { this._labelCreated = true; } + removeLabel() { + if(!this._container || !this._labelCreated) { + return; + } + + this._label!.remove(); + this._label = undefined; + this._labelCreated = false; + } + update(options: GenericIconOptions) { if(this._image && options.icon !== this.options.icon) { this._image!.src = useStore().state.currentMapProvider!.getMarkerIconUrl(this.options.icon); diff --git a/src/leaflet/layer/LiveAtlasLayerGroup.ts b/src/leaflet/layer/LiveAtlasLayerGroup.ts index 6b975e5..e1e6be7 100644 --- a/src/leaflet/layer/LiveAtlasLayerGroup.ts +++ b/src/leaflet/layer/LiveAtlasLayerGroup.ts @@ -108,15 +108,17 @@ export default class LiveAtlasLayerGroup extends LayerGroup { update(options: LiveAtlasLayerGroupOptions) { if(this.options.showLabels !== options.showLabels) { - //Create labels if they are now always visible - //TODO: This will be slow when many markers exist. Is it worth doing? - if(options.showLabels) { - this.eachLayer((layer) => { - if(layer instanceof GenericMarker) { + this.eachLayer((layer) => { + //Create labels if they are now always visible + //TODO: This will be slow when many markers exist. Is it worth doing? + if(layer instanceof GenericMarker) { + if(options.showLabels) { (layer as GenericMarker).createLabel(); + } else { + (layer as GenericMarker).removeLabel(); } - }); - } + } + }); this.options.showLabels = options.showLabels; } diff --git a/src/leaflet/marker/GenericMarker.ts b/src/leaflet/marker/GenericMarker.ts index 1eed0f4..b46d156 100644 --- a/src/leaflet/marker/GenericMarker.ts +++ b/src/leaflet/marker/GenericMarker.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import {MarkerOptions, Marker, Util, LatLngExpression, Icon} from 'leaflet'; +import {MarkerOptions, Marker, LatLngExpression, Icon, Map} from 'leaflet'; import {LiveAtlasMarker} from "@/index"; import {GenericIcon} from "@/leaflet/icon/GenericIcon"; @@ -53,4 +53,16 @@ export class GenericMarker extends Marker { createLabel(): void { this.options.icon.createLabel(); } + + removeLabel(): void { + this.options.icon.createLabel(); + } + + onRemove(map: Map): this { + this.options.icon.removeLabel(); + + super.onRemove(map); + + return this; + } }