smeijer / leaflet-geosearch

A geocoding/address-lookup library supporting various api providers.
https://smeijer.github.io/leaflet-geosearch/
MIT License
1.02k stars 270 forks source link

How to set zoom level to be more zoomed in on search #304

Open Daniel-Bozinovski opened 2 years ago

Daniel-Bozinovski commented 2 years ago

Is there any way to be able to set the zoom level when you have searched for a specific place?

missinglink commented 2 years ago

Looking at the code...

By default the map will zoom to fit the bounding box of the geometry (as defined by the 'bounds' property coming back from the geocoding provider).

For geometries without a 'bounds' property, (such as point-based geometries), the L.LatLng.toBounds() function is called with sizeInMeters=10, this is presumably very small and would trigger a max zoom.

https://github.com/smeijer/leaflet-geosearch/blob/develop/src/SearchControl.ts#L437

Once you've reached Leaflet's maximum zoom you cannot zoom any further.

Please provide more information about what you're trying to do and what data you're using.

There doesn't appear to be any simple way with the existing code to 'hack it', I didn't try it but maybe you can manually zoom and then set retainZoomLevel?

Daniel-Bozinovski commented 2 years ago

Hey thanks for the reply!

So after I search for a location, the leaflet map re-centres on the address with some zoom level set automatically by geosearch. I want it to be more zoomed in (maybe around 20 zoom level) on the searched location but I'm not sure how to manually set it if you understand what I mean..

It's currently like this after I search for an address

Screen Shot 2021-11-16 at 11 28 04 am

but I want it to be like this

Screen Shot 2021-11-16 at 11 28 32 am
smeijer commented 2 years ago

I think you'd be able to set updateMap to false, so that the control doesn't zoom to the location at all. And next, you'll add your own handler to zoom/pan to the location on a match.

map.on('geosearch/showlocation', yourEventHandler);