Как отцентрировать карту по скоплению близлежащих маркеров? #257
-
Добрый день! Прошу помочь разобраться. Ранее был следующий код: он позволял отцентрировать карту по скоплению маркеров в одном городе при инициализации карты и также при изменении текущего города. Версия была установлена следующая: `"vue-yandex-maps": "^0.12.7" const initHandler = (newMap: typeof yandexMap) => {
map = newMap
recenter()
}
const recenter = () => {
setTimeout(() => {
map.setBounds(map.geoObjects.getBounds()).then(() => {
if (map.getZoom() > 16) { map.setZoom(16) }
})
}, 300)
}
watch(currentCity, () => {
recenter()
}) Каким образом можно это реализовать в "vue-yandex-maps": "^2.0.1"? Прочитав документацию, я нашёл промис, который поможет мне отреагировать на инициализацию карты. Каким образом я могу сделать всё остальное? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Хаюшки! Кроме geoObjects всё просто, и промис вам не нужен. <template>
<yandex-map v-model="map">...</yandex-map>
</template>
<script setup lang="ts">
import { shallowRef } from 'vue';
import type { YMap } from '@yandex/ymaps3-types';
const map = shallowRef<YMap | null>(null);
const recenter = () => {
if (!map.value) return;
//setTimeout это был костыль наверное?
map.value.setLocation({ bounds: getAllMarkersBounds() }).then(() => {
if (map.value!.zoom > 16) { map.value!.setLocation({ zoom: 16 }) }
})
}
watch([map, currentCity], () => {
recenter();
});
</script> По поводу geoObjects и getAllMarkersBounds. Я не знаю простых способов получить эту информацию в Картах 3.0. Я предлагаю вам посчитать это самостоятельно, используя доступную вам информацию. Пример таких рассчетов есть у меня в кластере: https://github.com/yandex-maps-unofficial/vue-yandex-maps/blob/master/packages/vue-yandex-maps/src/components/modules/clusterer/YandexMapClustererClusters.vue#L82 |
Beta Was this translation helpful? Give feedback.
Хаюшки! Кроме geoObjects всё просто, и промис вам не нужен.
По поводу geoObjects и getAllMarkersBounds. Я не знаю простых способов получить эту информацию в Картах …