Skip to content

Commit

Permalink
map broken in Map Settings Page fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
abhilash-aot committed Jul 25, 2024
1 parent 01cb3d5 commit e11174f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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);
// 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

0 comments on commit e11174f

Please sign in to comment.