flutter_map icon indicating copy to clipboard operation
flutter_map copied to clipboard

Finding center and zoom level in given a list of lat-long pairs

Open Lyazz opened this issue 2 years ago • 4 comments

How do I find what should I set as the center and as the zoom level so that all the points show up.

Lyazz avatar Feb 01 '23 15:02 Lyazz

Depends on your tile layers and the details they represent.

xclud avatar Feb 01 '23 21:02 xclud

I used the RasterMap one, I got 5 points on the map, i find the center by calculating the average LatLng thought it is rightly centered but the zoom is not optimal. thanks.

Lyazz avatar Feb 02 '23 11:02 Lyazz

To get the zoom level, you'll need to know the pixel dimensions of your map. You'll also need to do your math in spherical mercator coordinates.

Convert latitude, longitude to spherical mercator x, y. Get distance between your two points in spherical mercator. The equator is about 40m meters long projected and tiles are 256 pixels wide, so the pixel length of that map at a given zoom level is about 256 * distance/40000000 * 2^zoom. Try zoom=0, zoom=1, zoom=2 until the distance is too long for the pixel dimensions of your map.

Source: https://gis.stackexchange.com/questions/19632/how-to-calculate-the-optimal-zoom-level-to-display-two-or-more-points-on-a-map

I hope soon i will add this feature to the package.

xclud avatar Feb 02 '23 14:02 xclud

Thanks mate.

Lyazz avatar Feb 05 '23 13:02 Lyazz