Skip to content

Commit 05bb19f

Browse files
authored
Rework map bounds (#231)
Rework map bounds... - drop omitFromFit - add zoom_changed, center_changed, and view_changed events - change version to 0.0.4 (definitely not stable)
1 parent 70521b7 commit 05bb19f

8 files changed

Lines changed: 228 additions & 60 deletions

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This project is a fork of [lit-google-map](https://github.com/arkadiuszwojcik/li
66
- add marker `mouseover` and `mouseout` events
77
- fix marker `icon` attribute not being updateable live
88
- update dependencies and keep current with Dependabot
9-
- add `omit-from-fit` to markers. This lets you add more markers to display on the map that won't affect the sizing of the bounds box
9+
- add `zoom_changed`, `center_changed`, and `view_changed` events
1010
- move to [AdvancedMarkerElement](https://developers.google.com/maps/documentation/javascript/advanced-markers/migration)
1111

1212
## Table of contents
@@ -109,6 +109,9 @@ Example:
109109

110110
- '_bounds_changed_' - Custom Event with `detail` attribute containing `north`, `south`, `east`, and `west` attributes
111111
- '_tilesloaded_' - Custom Event containing the same data as `bounds_changed`
112+
- '_center_changed_' - Custom Event with `detail` attribute containing `lat` and `lng` attributes
113+
- '_zoom_changed_' - Custom Event with `detail` attribute containing `zoom` attribute
114+
- '_view_changed_' - Custom Event with `detail` attribute containing `center` (with `lat` and `lng`) and `zoom` attributes. Fires whenever either center or zoom changes
112115
- '_place_click_' - Custom Event containing `detail` attribute containing `placeId` attribute
113116

114117
## Marker events
@@ -121,7 +124,6 @@ Example:
121124

122125
- '_latitude_' - Marker latitude position
123126
- '_longitude_' - Marker longitude position
124-
- '_omit-from-fit_' - Don't include marker when fitting map bounds to markers
125127
- '_z-index_' - Marker z index
126128
- '_id_' - Use with Marker events to identify the source
127129
- '_glyph_' - Glyph in center of pin

dist/lit-google-map.bundle.js

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ var LitGoogleMap = (function (exports) {
8888
this.mapId = "DEMO_MAP_ID";
8989
this.map = null;
9090
}
91+
attributeChangedCallback(name, oldValue, newValue) {
92+
super.attributeChangedCallback(name, oldValue, newValue);
93+
if (this.map && oldValue !== newValue && newValue !== null) {
94+
if (name === "center-latitude" || name === "center-longitude") {
95+
this.map.setCenter({
96+
lat: this.centerLatitude,
97+
lng: this.centerLongitude,
98+
});
99+
}
100+
else if (name === "zoom") {
101+
this.map.setZoom(this.zoom);
102+
}
103+
}
104+
}
105+
dispatchViewChanged() {
106+
this.dispatchEvent(new CustomEvent("view_changed", {
107+
detail: {
108+
center: this.map.getCenter().toJSON(),
109+
zoom: this.map.getZoom(),
110+
},
111+
bubbles: true,
112+
composed: true,
113+
}));
114+
}
91115
initGMap() {
92116
if (this.map != null) {
93117
return;
@@ -111,6 +135,22 @@ var LitGoogleMap = (function (exports) {
111135
composed: true,
112136
}));
113137
});
138+
this.map.addListener("center_changed", () => {
139+
this.dispatchEvent(new CustomEvent("center_changed", {
140+
detail: this.map.getCenter().toJSON(),
141+
bubbles: true,
142+
composed: true,
143+
}));
144+
this.dispatchViewChanged();
145+
});
146+
this.map.addListener("zoom_changed", () => {
147+
this.dispatchEvent(new CustomEvent("zoom_changed", {
148+
detail: { zoom: this.map.getZoom() },
149+
bubbles: true,
150+
composed: true,
151+
}));
152+
this.dispatchViewChanged();
153+
});
114154
this.map.addListener("click", (event) => {
115155
if ("placeId" in event) {
116156
event.stop();
@@ -140,6 +180,27 @@ var LitGoogleMap = (function (exports) {
140180
super.connectedCallback();
141181
this.initGMap();
142182
}
183+
updated(changedProperties) {
184+
super.updated(changedProperties);
185+
if (this.map) {
186+
if (changedProperties.has("centerLatitude") ||
187+
changedProperties.has("centerLongitude")) {
188+
changedProperties.get("centerLatitude");
189+
changedProperties.get("centerLongitude");
190+
this.map.setCenter({
191+
lat: this.centerLatitude,
192+
lng: this.centerLongitude,
193+
});
194+
}
195+
if (changedProperties.has("zoom")) {
196+
changedProperties.get("zoom");
197+
this.map.setZoom(this.zoom);
198+
}
199+
}
200+
else {
201+
console.log("Map not initialized yet, skipping update");
202+
}
203+
}
143204
attachChildrenToMap(children) {
144205
if (this.map) {
145206
for (const child of children) {
@@ -197,10 +258,9 @@ var LitGoogleMap = (function (exports) {
197258
}
198259
}
199260
fitToMarkersChanged(retryAttempt = 0) {
200-
const markers = this.markers.filter((m) => !m.omitFromFit);
201-
if (this.map && this.fitToMarkers && markers.length > 0) {
261+
if (this.map && this.fitToMarkers && this.markers.length > 0) {
202262
const latLngBounds = new google.maps.LatLngBounds();
203-
for (const marker of markers) {
263+
for (const marker of this.markers) {
204264
latLngBounds.extend(new google.maps.LatLng(marker.latitude, marker.longitude));
205265
}
206266
const domDimensions = this.getBoundingClientRect();
@@ -212,20 +272,18 @@ var LitGoogleMap = (function (exports) {
212272
}, timeout);
213273
return;
214274
}
215-
if (markers.length > 1) {
275+
if (this.markers.length > 1) {
216276
this.map.fitBounds(latLngBounds, 0);
217277
}
218278
this.map.setCenter(latLngBounds.getCenter());
219279
}
220280
}
221281
checkBoundsChanged(oldMarkers, newMarkers) {
222282
const addedInBounds = newMarkers.filter((m) => {
223-
return (!m.omitFromFit &&
224-
(!oldMarkers || !oldMarkers.includes(m)));
283+
return !oldMarkers || !oldMarkers.includes(m);
225284
});
226285
const removedInBounds = oldMarkers === null || oldMarkers === void 0 ? void 0 : oldMarkers.filter((m) => {
227-
return (!m.omitFromFit &&
228-
(!newMarkers || !newMarkers.includes(m)));
286+
return !newMarkers || !newMarkers.includes(m);
229287
});
230288
return addedInBounds.length > 0 || removedInBounds.length > 0;
231289
}
@@ -426,7 +484,6 @@ var LitGoogleMap = (function (exports) {
426484
this.background = null;
427485
this.borderColor = null;
428486
this.scale = null;
429-
this.omitFromFit = false;
430487
this.map = null;
431488
this.marker = null;
432489
this.pin = null;
@@ -625,10 +682,6 @@ var LitGoogleMap = (function (exports) {
625682
n({ type: Number, reflect: true }),
626683
__metadata("design:type", Number)
627684
], exports.LitGoogleMapMarker.prototype, "scale", void 0);
628-
__decorate([
629-
n({ type: Boolean, attribute: "omit-from-fit" }),
630-
__metadata("design:type", Object)
631-
], exports.LitGoogleMapMarker.prototype, "omitFromFit", void 0);
632685
exports.LitGoogleMapMarker = __decorate([
633686
t("lit-google-map-marker")
634687
], exports.LitGoogleMapMarker);

0 commit comments

Comments
 (0)