update: center the marker on station select
This commit is contained in:
parent
55a7b32c76
commit
86b116dd97
@ -5,6 +5,7 @@ import {
|
|||||||
YMaps,
|
YMaps,
|
||||||
} from '@pbe/react-yandex-maps';
|
} from '@pbe/react-yandex-maps';
|
||||||
import { isEmpty } from 'lodash';
|
import { isEmpty } from 'lodash';
|
||||||
|
import { useEffect, useRef } from 'react';
|
||||||
|
|
||||||
import { Point } from '../model';
|
import { Point } from '../model';
|
||||||
|
|
||||||
@ -21,6 +22,29 @@ export const YandexMap = ({
|
|||||||
selectedStation,
|
selectedStation,
|
||||||
handleMapStationClick,
|
handleMapStationClick,
|
||||||
}: YandexMapProps) => {
|
}: YandexMapProps) => {
|
||||||
|
const mapRef = useRef<any>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!mapRef.current) return;
|
||||||
|
|
||||||
|
if (selectedStation !== null) {
|
||||||
|
const selectedPoint = points.find(
|
||||||
|
(point) => point.id === selectedStation,
|
||||||
|
);
|
||||||
|
if (selectedPoint) {
|
||||||
|
mapRef.current
|
||||||
|
.setCenter(selectedPoint.coordinates, mapRef.current.getZoom(), {
|
||||||
|
duration: 1000,
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
mapRef.current.setZoom(13, { duration: 300 });
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mapRef.current.setZoom(11, { duration: 300 });
|
||||||
|
}
|
||||||
|
}, [selectedStation, points]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<YMaps
|
<YMaps
|
||||||
query={{
|
query={{
|
||||||
@ -42,6 +66,9 @@ export const YandexMap = ({
|
|||||||
suppressObsoleteBrowserNotifier: true,
|
suppressObsoleteBrowserNotifier: true,
|
||||||
}}
|
}}
|
||||||
className='h-full max-h-[500px] w-full overflow-hidden rounded-md shadow-lg'
|
className='h-full max-h-[500px] w-full overflow-hidden rounded-md shadow-lg'
|
||||||
|
instanceRef={(ref) => {
|
||||||
|
mapRef.current = ref;
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{points.map((point) => {
|
{points.map((point) => {
|
||||||
const isSelectedStation = selectedStation === point.id;
|
const isSelectedStation = selectedStation === point.id;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user