Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix map broken in Map Settings Page #13

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions components/src/components/Map/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,19 @@ export default class Component extends (FieldComponent as any) {
const { numPoints, defaultZoom, readOnlyMap, center, defaultValue } =
this.component;


const { readOnly: viewMode } = this.options;

let initialCenter;
if (center && center.features && center.features[0]) {
initialCenter = center.features[0].coordinates;
} else {
initialCenter = DEFAULT_CENTER;
}

this.mapService = new MapService({
mapContainer,
drawOptions,
center: center ? initialCenter : DEFAULT_CENTER,
center: initialCenter,
form,
numPoints,
defaultZoom,
Expand Down
17 changes: 15 additions & 2 deletions components/src/components/Map/services/MapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ class MapService {
options;
map;
drawnItems;

constructor(options) {
this.options = options;

if (options.mapContainer) {
const { map, drawnItems } = this.initializeMap(options);
this.map = map;
this.drawnItems = drawnItems;

map.invalidateSize();
// Triggering a resize event after map initialization
setTimeout(() => window.dispatchEvent(new Event('resize')), 0);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great!

// Event listener for drawn objects
map.on('draw:created', (e) => {
let layer = e.layer;
Expand All @@ -54,6 +58,10 @@ class MapService {
map.on(L.Draw.Event.EDITED, (e) => {
options.onDrawnItemsChange(drawnItems.getLayers());
});

map.on('resize', () => {
map.invalidateSize();
});
}
}

Expand All @@ -68,22 +76,23 @@ class MapService {
viewMode,
} = options;


if (drawOptions.rectangle) {
drawOptions.rectangle.showArea = false;
}

const map = L.map(mapContainer).setView(
center,
defaultZoom || DEFAULT_MAP_ZOOM
);
L.tileLayer(DEFAULT_MAP_LAYER_URL, {
attribution: DEFAULT_LAYER_ATTRIBUTION,
}).addTo(map);

// Initialize Draw Layer
let drawnItems = new L.FeatureGroup();
map.addLayer(drawnItems);
// Add Drawing Controllers

// Add Drawing Controllers
if (!readOnlyMap) {
if (!viewMode) {
let drawControl = new L.Control.Draw({
Expand All @@ -95,6 +104,7 @@ class MapService {
map.addControl(drawControl);
}
}

// Checking to see if the map should be interactable
const componentEditNode =
document.getElementsByClassName(COMPONENT_EDIT_CLASS);
Expand All @@ -111,6 +121,7 @@ class MapService {
}
return { map, drawnItems };
}

bindPopupToLayer(layer) {
if (layer instanceof L.Marker) {
layer
Expand Down Expand Up @@ -140,6 +151,7 @@ class MapService {
.openPopup();
}
}

loadDrawnItems(items) {
const { drawnItems } = this;
if (!drawnItems) {
Expand Down Expand Up @@ -169,6 +181,7 @@ class MapService {
}
});
}

hasChildNode(parent, targetNode) {
if (parent === targetNode) {
return true;
Expand Down
Loading