Skip to content

Commit

Permalink
Fix marker labels disappearing when toggling a showLabels: true layer…
Browse files Browse the repository at this point in the history
… twice
  • Loading branch information
JLyne committed Aug 16, 2021
1 parent 552b289 commit 2d485ac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/leaflet/icon/GenericIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
16 changes: 9 additions & 7 deletions src/leaflet/layer/LiveAtlasLayerGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 13 additions & 1 deletion src/leaflet/marker/GenericMarker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 2d485ac

Please sign in to comment.