Skip to content

Commit

Permalink
Fix map zone focus issues (#22623)
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts authored Nov 4, 2024
1 parent 5f58c18 commit 4ef944e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/components/map/ha-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export class HaMap extends ReactiveElement {

private _mapZones: Array<Marker | Circle> = [];

private _mapFocusZones: Array<Marker | Circle> = [];

private _mapPaths: Array<Polyline | CircleMarker> = [];

public connectedCallback(): void {
Expand Down Expand Up @@ -201,7 +203,11 @@ export class HaMap extends ReactiveElement {
return;
}

if (!this._mapFocusItems.length && !this.layers?.length) {
if (
!this._mapFocusItems.length &&
!this._mapFocusZones.length &&
!this.layers?.length
) {
this.leafletMap.setView(
new this.Leaflet.LatLng(
this.hass.config.latitude,
Expand All @@ -218,13 +224,9 @@ export class HaMap extends ReactiveElement {
: []
);

if (this.fitZones) {
this._mapZones?.forEach((zone) => {
bounds.extend(
"getBounds" in zone ? zone.getBounds() : zone.getLatLng()
);
});
}
this._mapFocusZones?.forEach((zone) => {
bounds.extend("getBounds" in zone ? zone.getBounds() : zone.getLatLng());
});

this.layers?.forEach((layer: any) => {
bounds.extend(
Expand Down Expand Up @@ -395,6 +397,7 @@ export class HaMap extends ReactiveElement {
if (this._mapZones.length) {
this._mapZones.forEach((marker) => marker.remove());
this._mapZones = [];
this._mapFocusZones = [];
}

if (!this.entities) {
Expand Down Expand Up @@ -466,13 +469,18 @@ export class HaMap extends ReactiveElement {
);

// create circle around it
this._mapZones.push(
Leaflet.circle([latitude, longitude], {
interactive: false,
color: passive ? passiveZoneColor : zoneColor,
radius,
})
);
const circle = Leaflet.circle([latitude, longitude], {
interactive: false,
color: passive ? passiveZoneColor : zoneColor,
radius,
});
this._mapZones.push(circle);
if (
this.fitZones &&
(typeof entity === "string" || entity.focus !== false)
) {
this._mapFocusZones.push(circle);
}

continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const cardConfigStruct = assign(
hours_to_show: optional(number()),
geo_location_sources: optional(array(geoSourcesConfigStruct)),
auto_fit: optional(boolean()),
fit_zones: optional(boolean()),
theme_mode: optional(string()),
})
);
Expand Down

0 comments on commit 4ef944e

Please sign in to comment.