Skip to content

Commit 9b515c3

Browse files
committed
Add HTMLMapmlViewerElement.zoomToExtent(xmin,ymin,xmax,ymax) to support documentation change for custom search handler development
1 parent 27cb0e5 commit 9b515c3

5 files changed

Lines changed: 53 additions & 1 deletion

File tree

index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,17 @@
249249

250250
viewer.addEventListener('mapsearch', (e) => {
251251
e.preventDefault();
252-
e.detail.setResults(toItems(e.detail.responses, false));
252+
const items = toItems(e.detail.responses, false);
253+
e.detail.setResults(items);
254+
// Auto-navigate to the first result (matches default handler behaviour)
255+
if (items.length > 0) {
256+
const first = items[0];
257+
if (first.bbox && first.bbox.length === 4) {
258+
viewer.zoomToExtent(...first.bbox);
259+
} else if (first.lat != null && first.lng != null) {
260+
viewer.zoomTo(first.lat, first.lng, 14);
261+
}
262+
}
253263
});
254264

255265
});

src/mapml-viewer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,10 @@ export class HTMLMapmlViewerElement extends HTMLElement {
10821082
this.lat = location.lat;
10831083
this.lon = location.lng;
10841084
}
1085+
zoomToExtent(west, south, east, north) {
1086+
this._map.fitBounds(latLngBounds([+south, +west], [+north, +east]));
1087+
this._updateMapCenter();
1088+
}
10851089
_updateMapCenter() {
10861090
// remember to tell Leaflet event handler that 'this' in here refers to
10871091
// something other than the map in this case the custom polymer element

src/web-map.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,10 @@ export class HTMLWebMapElement extends HTMLMapElement {
11251125
this.lat = location.lat;
11261126
this.lon = location.lng;
11271127
}
1128+
zoomToExtent(west, south, east, north) {
1129+
this._map.fitBounds(latLngBounds([+south, +west], [+north, +east]));
1130+
this._updateMapCenter();
1131+
}
11281132
_updateMapCenter() {
11291133
// remember to tell Leaflet event handler that 'this' in here refers to
11301134
// something other than the map in this case the custom polymer element

test/e2e/mapml-viewer/mapml-viewer.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,25 @@ test.describe('Playwright mapml-viewer Element Tests', () => {
7777
expect(extent.topLeft.tcrs[0]).toEqual(expectedFirstTCRS[1]);
7878
});
7979

80+
test('zoomToExtent fits the map to the given bounds', async () => {
81+
// zoom to a known bbox (west, south, east, north) and verify the map
82+
// center and zoom update accordingly
83+
await page.$eval('body > mapml-viewer', (map) =>
84+
map.zoomToExtent(-80, 43, -70, 48)
85+
);
86+
await page.waitForTimeout(500);
87+
const result = await page.$eval('body > mapml-viewer', (map) => ({
88+
lat: map.lat,
89+
lon: map.lon,
90+
zoom: map.zoom
91+
}));
92+
// center should be approximately (45.5, -75) — the midpoint of the bbox
93+
expect(result.lat).toBeCloseTo(45.5, 0);
94+
expect(result.lon).toBeCloseTo(-75, 0);
95+
// zoom should have increased from the previous zoom level
96+
expect(result.zoom).toBeGreaterThan(1);
97+
});
98+
8099
test.describe('Attributes Tests', () => {
81100
for (let i in controls) {
82101
test.describe('Controls List ' + options[i] + ' Attribute Tests', () => {

test/e2e/web-map/map.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ test.describe('Playwright web-map Element Tests', () => {
146146
expect(currPos).toEqual(0);
147147
});
148148

149+
test('zoomToExtent fits the map to the given bounds', async () => {
150+
await page.$eval('body > map', (map) => map.zoomToExtent(-80, 43, -70, 48));
151+
await page.waitForTimeout(500);
152+
const result = await page.$eval('body > map', (map) => ({
153+
lat: map.lat,
154+
lon: map.lon,
155+
zoom: map.zoom
156+
}));
157+
// center should be approximately (45.5, -75) — the midpoint of the bbox
158+
expect(result.lat).toBeCloseTo(45.5, 0);
159+
expect(result.lon).toBeCloseTo(-75, 0);
160+
// zoom should have increased from the initial zoom level
161+
expect(result.zoom).toBeGreaterThan(1);
162+
});
163+
149164
test.describe('Controls List search Attribute Tests', () => {
150165
test('controlslist=search shows search control', async () => {
151166
await page.$eval('body > map', (map) =>

0 commit comments

Comments
 (0)