Skip to content

Commit

Permalink
Merge branch 'master' into feature-arcgis-server-support
Browse files Browse the repository at this point in the history
  • Loading branch information
underbluewaters committed Oct 18, 2023
2 parents 6a2fa0c + 016cef6 commit cc436fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
33 changes: 20 additions & 13 deletions packages/client/src/MeasureControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,14 @@ class MeasureControl extends EventEmitter {
private addSourcesAndLayers() {
const sources = this.getSources();
for (const id in sources) {
this.map.addSource(id, sources[id]);
if (!this.map.getSource(id)) {
this.map.addSource(id, sources[id]);
}
}
for (const layer of measureLayers) {
this.map.addLayer(layer);
if (!this.map.getLayer(layer.id)) {
this.map.addLayer(layer);
}
}
}

Expand Down Expand Up @@ -297,24 +301,27 @@ class MeasureControl extends EventEmitter {
* map. Call before discarding this instance of MeasureControl.
*/
destroy = () => {
for (const layer of measureLayers) {
this.map.removeLayer(layer.id);
}
const sources = this.getSources();
for (const id in sources) {
if (this.map.getSource(id)) {
this.map.removeSource(id);
if (this.map && this.map.loaded()) {
for (const layer of measureLayers) {
this.map.removeLayer(layer.id);
}
const sources = this.getSources();
for (const id in sources) {
if (this.map.getSource(id)) {
this.map.removeSource(id);
}
}
this.map.doubleClickZoom.enable();
// unregister all event handlers
this.removeEventListeners(this.map);
}
this.map.doubleClickZoom.enable();
// unregister all event handlers
this.removeEventListeners(this.map);
document.body.removeEventListener("keydown", this.onKeyDown);
this.isDestroyed = true;
};

onKeyDown = (e: KeyboardEvent) => {
if (this.isDestroyed) {
throw new Error("MeasureControl is destroyed");
return;
}
if (e.key === "Escape" && this.state === "drawing") {
this.stopEditing();
Expand Down
25 changes: 18 additions & 7 deletions packages/client/src/draw/useMapboxGLDraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default function useMapboxGLDraw(
useEffect(() => {
if (
mapContext.manager?.map &&
// mapContext.manager.map.loaded() &&
geometryType &&
!disabled &&
!handlerState.current.draw
Expand Down Expand Up @@ -188,14 +189,23 @@ export default function useMapboxGLDraw(
.coordinates as [number, number]
);
} else {
map.fitBounds(
bbox(initialValue) as [number, number, number, number],
{
padding: isSmall ? 100 : 200,
animate: true,
duration: 500,
const bounds = bbox(initialValue) as [
number,
number,
number,
number
];
try {
if (bounds && !Number.isNaN(bounds[0])) {
map.fitBounds(bounds, {
padding: isSmall ? 100 : 200,
animate: true,
duration: 500,
});
}
);
} catch (e) {
// do nothing
}
}
}
} else {
Expand Down Expand Up @@ -357,6 +367,7 @@ export default function useMapboxGLDraw(
disabled,
preprocessingEndpoint,
preprocessingResults,
// mapContext.manager?.map?.loaded(),
]);

useEffect(() => {
Expand Down

0 comments on commit cc436fb

Please sign in to comment.