Fix marker labels disappearing when toggling a showLabels: true layer twice

This commit is contained in:
James Lyne 2021-08-16 04:17:59 +01:00
parent 552b289bb9
commit 2d485ac4e5
3 changed files with 32 additions and 8 deletions

View File

@ -117,6 +117,16 @@ export class GenericIcon extends DivIcon {
this._labelCreated = true; this._labelCreated = true;
} }
removeLabel() {
if(!this._container || !this._labelCreated) {
return;
}
this._label!.remove();
this._label = undefined;
this._labelCreated = false;
}
update(options: GenericIconOptions) { update(options: GenericIconOptions) {
if(this._image && options.icon !== this.options.icon) { if(this._image && options.icon !== this.options.icon) {
this._image!.src = useStore().state.currentMapProvider!.getMarkerIconUrl(this.options.icon); this._image!.src = useStore().state.currentMapProvider!.getMarkerIconUrl(this.options.icon);

View File

@ -108,15 +108,17 @@ export default class LiveAtlasLayerGroup extends LayerGroup {
update(options: LiveAtlasLayerGroupOptions) { update(options: LiveAtlasLayerGroupOptions) {
if(this.options.showLabels !== options.showLabels) { if(this.options.showLabels !== options.showLabels) {
this.eachLayer((layer) => {
//Create labels if they are now always visible //Create labels if they are now always visible
//TODO: This will be slow when many markers exist. Is it worth doing? //TODO: This will be slow when many markers exist. Is it worth doing?
if(options.showLabels) {
this.eachLayer((layer) => {
if(layer instanceof GenericMarker) { if(layer instanceof GenericMarker) {
if(options.showLabels) {
(layer as GenericMarker).createLabel(); (layer as GenericMarker).createLabel();
} else {
(layer as GenericMarker).removeLabel();
}
} }
}); });
}
this.options.showLabels = options.showLabels; this.options.showLabels = options.showLabels;
} }

View File

@ -14,7 +14,7 @@
* limitations under the License. * 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 {LiveAtlasMarker} from "@/index";
import {GenericIcon} from "@/leaflet/icon/GenericIcon"; import {GenericIcon} from "@/leaflet/icon/GenericIcon";
@ -53,4 +53,16 @@ export class GenericMarker extends Marker {
createLabel(): void { createLabel(): void {
this.options.icon.createLabel(); this.options.icon.createLabel();
} }
removeLabel(): void {
this.options.icon.createLabel();
}
onRemove(map: Map): this {
this.options.icon.removeLabel();
super.onRemove(map);
return this;
}
} }