@@ -249,6 +249,10 @@ function initializeAutocomplete() {
249249 } ,
250250 select : function ( event , ui ) {
251251 var selectedEntity = ui . item . properties . address . formattedAddress ;
252+
253+ // Update the search textbox with the selected suggestion
254+ document . getElementById ( 'searchTbx' ) . value = selectedEntity ;
255+
252256 geocodeSelectedLocation ( selectedEntity , ui . item ) ;
253257 return false ;
254258 }
@@ -278,31 +282,123 @@ async function geocodeSelectedLocation(selectedEntity, autocompleteItem) {
278282
279283 if ( json . features && json . features . length > 0 ) {
280284 var result = json . features [ 0 ] ;
281- var coordinates = result . geometry . coordinates ;
282-
283- var bbox = [
284- coordinates [ 0 ] ,
285- coordinates [ 1 ] ,
286- coordinates [ 0 ] ,
287- coordinates [ 1 ]
288- ] ;
289-
290- map . setCamera ( {
291- bounds : bbox ,
292- padding : 50
293- } ) ;
285+ var coordinates = result . geometry && result . geometry . coordinates ;
294286
287+ if ( ! coordinates ) {
288+ return ;
289+ }
290+
291+ // Update center marker position
295292 centerMarker . setOptions ( {
296- position : coordinates
293+ position : coordinates ,
294+ visible : true
297295 } ) ;
298296
299- updateListItems ( ) ;
297+ // Adjust map view to include the center and nearest stores
298+ fitMapToNearestStores ( coordinates , 5 ) ;
300299 }
301300 } catch ( error ) {
302301 console . error ( 'Geocoding error:' , error ) ;
303302 }
304303}
305304
305+ function fitMapToNearestStores ( center , nearestCount = 5 ) {
306+ // Get all shapes (stores) from datasource
307+ var shapes = datasource && datasource . getShapes ? datasource . getShapes ( ) : [ ] ;
308+
309+ if ( ! shapes || shapes . length === 0 ) {
310+ // Fallback if data not yet loaded
311+ map . setCamera ( {
312+ center : center ,
313+ zoom : 10
314+ } ) ;
315+ return ;
316+ }
317+
318+ // Collect non-cluster store shapes
319+ var storeShapes = [ ] ;
320+ for ( var i = 0 ; i < shapes . length ; i ++ ) {
321+ var s = shapes [ i ] ;
322+ if ( s instanceof atlas . Shape ) {
323+ var props = s . getProperties ( ) || { } ;
324+ // Exclude clusters (those that have point_count)
325+ if ( ! props . point_count ) {
326+ storeShapes . push ( s ) ;
327+ }
328+ }
329+ }
330+
331+ if ( storeShapes . length === 0 ) {
332+ map . setCamera ( {
333+ center : center ,
334+ zoom : 10
335+ } ) ;
336+ return ;
337+ }
338+
339+ // Compute distance to each store from the geocoded center
340+ var centerPos = center ;
341+ storeShapes . sort ( function ( a , b ) {
342+ var da = atlas . math . getDistanceTo ( centerPos , a . getCoordinates ( ) , 'miles' ) ;
343+ var db = atlas . math . getDistanceTo ( centerPos , b . getCoordinates ( ) , 'miles' ) ;
344+ return da - db ;
345+ } ) ;
346+
347+ // Check distance to nearest store
348+ var nearestDistance = atlas . math . getDistanceTo ( centerPos , storeShapes [ 0 ] . getCoordinates ( ) , 'miles' ) ;
349+
350+ // If nearest store is more than 10 miles away, use default geocode zoom view
351+ if ( nearestDistance > 10 ) {
352+ map . setCamera ( {
353+ center : center ,
354+ zoom : 12 // Default geocode zoom level
355+ } ) ;
356+ return ;
357+ }
358+
359+ // If nearest stores are within 10 miles, continue with the existing logic
360+ var selected = storeShapes . slice ( 0 , Math . min ( nearestCount , storeShapes . length ) ) ;
361+
362+ // Include center in bounds computation
363+ var minLon = centerPos [ 0 ] , maxLon = centerPos [ 0 ] ,
364+ minLat = centerPos [ 1 ] , maxLat = centerPos [ 1 ] ;
365+
366+ for ( var j = 0 ; j < selected . length ; j ++ ) {
367+ var c = selected [ j ] . getCoordinates ( ) ;
368+ if ( c [ 0 ] < minLon ) minLon = c [ 0 ] ;
369+ if ( c [ 0 ] > maxLon ) maxLon = c [ 0 ] ;
370+ if ( c [ 1 ] < minLat ) minLat = c [ 1 ] ;
371+ if ( c [ 1 ] > maxLat ) maxLat = c [ 1 ] ;
372+ }
373+
374+ // Add padding in degrees (ensures not too tight)
375+ var lonSpan = maxLon - minLon ;
376+ var latSpan = maxLat - minLat ;
377+
378+ var padLon = lonSpan * 0.15 ;
379+ var padLat = latSpan * 0.15 ;
380+
381+ // If span is extremely small (e.g., all points same), enforce a minimal span
382+ if ( lonSpan < 0.01 ) padLon = 0.01 ;
383+ if ( latSpan < 0.01 ) padLat = 0.01 ;
384+
385+ var bounds = [
386+ minLon - padLon ,
387+ minLat - padLat ,
388+ maxLon + padLon ,
389+ maxLat + padLat
390+ ] ;
391+
392+ map . setCamera ( {
393+ bounds : bounds ,
394+ padding : 60
395+ } ) ;
396+
397+ // The render event will trigger updateListItems; optionally force an early update
398+ setTimeout ( updateListItems , 100 ) ;
399+ }
400+
401+
306402function performSearch ( ) {
307403 var query = document . getElementById ( 'searchTbx' ) . value ;
308404
0 commit comments