added card on post and feed
This commit is contained in:
102
src/hooks/useYandexMap.js
Normal file
102
src/hooks/useYandexMap.js
Normal file
@@ -0,0 +1,102 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
|
||||
export function useYandexMap(enabled, initialCoords = null, initialAddress = "") {
|
||||
const mapRef = useRef(null);
|
||||
const ymapRef = useRef(null);
|
||||
const placemarkRef = useRef(null);
|
||||
const initializedRef = useRef(false); // защита от двойного вызова StrictMode
|
||||
|
||||
const [coords, setCoords] = useState(initialCoords);
|
||||
const [address, setAddress] = useState(initialAddress ?? "");
|
||||
const addressRef = useRef(initialAddress ?? ""); // ← добавьте
|
||||
|
||||
|
||||
const resolveAddress = (lat, lng) => {
|
||||
fetch(
|
||||
`https://geocode-maps.yandex.ru/1.x/?apikey=c2d3c717-a109-4494-8863-7edbc32044e3&geocode=${lng},${lat}&format=json&lang=ru_RU`
|
||||
)
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
const found =
|
||||
data?.response?.GeoObjectCollection?.featureMember?.[0]
|
||||
?.GeoObject?.metaDataProperty?.GeocoderMetaData?.text;
|
||||
const resolved = found || `${lat.toFixed(5)}, ${lng.toFixed(5)}`;
|
||||
setAddress(resolved);
|
||||
addressRef.current = resolved;
|
||||
})
|
||||
.catch(() => {
|
||||
const fallback = `${lat.toFixed(5)}, ${lng.toFixed(5)}`;
|
||||
setAddress(fallback);
|
||||
addressRef.current = fallback;
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!enabled || !mapRef.current) return;
|
||||
if (initializedRef.current) return; // уже инициализировали
|
||||
initializedRef.current = true;
|
||||
|
||||
const init = () => {
|
||||
// Если карта уже создана — не создаём снова
|
||||
if (ymapRef.current) return;
|
||||
|
||||
const center = initialCoords
|
||||
? [initialCoords.latitude, initialCoords.longitude]
|
||||
: [55.751574, 37.573856];
|
||||
|
||||
const map = new window.ymaps.Map(mapRef.current, {
|
||||
center,
|
||||
zoom: 14,
|
||||
controls: ["zoomControl", "geolocationControl"],
|
||||
});
|
||||
ymapRef.current = map;
|
||||
|
||||
if (initialCoords) {
|
||||
const pm = new window.ymaps.Placemark(center, {}, { draggable: true });
|
||||
map.geoObjects.add(pm);
|
||||
placemarkRef.current = pm;
|
||||
resolveAddress(initialCoords.latitude, initialCoords.longitude);
|
||||
|
||||
pm.events.add("dragend", () => {
|
||||
const [lat, lng] = pm.geometry.getCoordinates();
|
||||
setCoords({ latitude: lat, longitude: lng });
|
||||
resolveAddress(lat, lng);
|
||||
});
|
||||
}
|
||||
|
||||
map.events.add("click", e => {
|
||||
const [lat, lng] = e.get("coords");
|
||||
setCoords({ latitude: lat, longitude: lng });
|
||||
resolveAddress(lat, lng);
|
||||
|
||||
if (placemarkRef.current) {
|
||||
placemarkRef.current.geometry.setCoordinates([lat, lng]);
|
||||
} else {
|
||||
const pm = new window.ymaps.Placemark([lat, lng], {}, { draggable: true });
|
||||
map.geoObjects.add(pm);
|
||||
placemarkRef.current = pm;
|
||||
|
||||
pm.events.add("dragend", () => {
|
||||
const [la, ln] = pm.geometry.getCoordinates();
|
||||
setCoords({ latitude: la, longitude: ln });
|
||||
resolveAddress(la, ln);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (window.ymaps?.ready) {
|
||||
window.ymaps.ready(init);
|
||||
}
|
||||
|
||||
return () => {
|
||||
// Уничтожаем карту только при размонтировании компонента
|
||||
ymapRef.current?.destroy();
|
||||
ymapRef.current = null;
|
||||
placemarkRef.current = null;
|
||||
initializedRef.current = false;
|
||||
};
|
||||
}, [enabled]);
|
||||
|
||||
return { mapRef, coords, address, addressRef };
|
||||
}
|
||||
Reference in New Issue
Block a user