If there are a lot of photos, displaying them all at once on the map is not the right choice for UX and performance. So let's group the photos and display their count.
For grouping OpenLayers has a cluster (ol.source.Cluster). It works with points (ol.geom.Point) out of the box, so it won't be difficult to add it for our task. Set the distance and where to get the points from (photosSource), and rename layerThumbs to layerClusters:
The selection logic can be enhanced by adding smooth zooming on a cluster click:
map.on('click',event=>{layerClusters.getFeatures(event.pixel).then(clickedFeatures=>{if(!clickedFeatures.length)return;constfeatures=clickedFeatures[0].get('features');if(features.length>1&&!isMaximumZoom(map.getView())){// Zoom inconstextent=ol.extent.boundingExtent(features.map(r=>r.getGeometry().getCoordinates()),);map.getView().fit(extent,{duration:400,padding:[150,150,150,150]});}else{// Show photo detailsconstdetails=features.map(f=>photoDetails(f));document.getElementById('photo-details').innerHTML=details.join();}});});functionisMaximumZoom(view){constmaxZoom=view.getMaxZoom();constcurrentZoom=view.getZoom();returncurrentZoom>=maxZoom;}
If a circle with 2+ elements is selected and there is still room to enlarge the map, we iterate through the selected coordinates and form a virtual extent area from them: