A
A
alex4answ2021-01-05 19:31:27
JavaScript
alex4answ, 2021-01-05 19:31:27

How to calculate the center and zoom for Yandex maps?

Good afternoon, I ran into a problem, I used to somehow solve it, now I can’t find it anymore, and the old solutions that I google don’t work.

There are n points, how to calculate the bounds and zoom for them so that they all fit on the map?

<YMaps>
  <Map
    defaultState={{
      zoom: 10,
      center: houses[0].position,
    }}
  >
    {houses.map((house) => <Placemark geometry={house.position} key={house.key} />}
  </Map>
</YMaps>


How is it implemented now?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alex4answ, 2021-01-05
@alex4answ

For me and people like me in the future:
1. Add all labels to the map
2. Get the bounds of the map instance: myMap.geoObjects.getBounds()
3. Assign it to the map instance, with options for zoom, padding, etc:

myMap.setBounds(
  myMap.geoObjects.getBounds(),
  { 
    checkZoomRange: true,
    zoomMargin: 9
  }
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question