react-leaflet의 마커 회전
10486 단어 leafletReact-Leaflet
RotableMarker.tsx
import { useEffect, useRef } from "react"; import L from "leaflet"; import { Marker, MarkerProps } from "react-leaflet"; type RotableMarkerProps = MarkerProps & { rotationAngle: number; rotationOrigin: string; }; export const RotableMarker: React.FC<RotableMarkerProps> = (props) => { const markerRef = useRef<any>(); useEffect(() => { const marker = markerRef.current; if (marker) { const proto_setPos = marker._setPos; marker._setPos = (pos: any) => { proto_setPos.call(marker, pos); if (props.rotationAngle) { marker._icon.style[L.DomUtil.TRANSFORM + "Origin"] = props.rotationOrigin; marker._icon.style[L.DomUtil.TRANSFORM] += " rotateZ(" + props.rotationAngle + "deg)"; } }; marker.update(); } }, [props.rotationAngle, props.rotationOrigin]); return ( <Marker ref={markerRef} {...props}> {props.children} </Marker> ); }; 사용법으로는 다음이 예입니다.
rotationAngle에서 회전 각도를 45도, rotationOrigin에서 CSS의 transform-origin을 지정합니다.
App.tsx
import L, { LatLngExpression } from "leaflet"; import "leaflet/dist/leaflet.css"; import { MapContainer, TileLayer } from "react-leaflet"; import { RotableMarker } from "./RotableMarker"; L.Icon.Default.imagePath = "//cdnjs.cloudflare.com/ajax/libs/leaflet/1.3.1/images/"; const App = () => { const position: LatLngExpression = [51.505, -0.09]; return ( <MapContainer center={position} zoom={13} scrollWheelZoom={false}> <TileLayer attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> <RotableMarker position={position} rotationAngle={45} rotationOrigin="center" /> </MapContainer> ); }; export default App; 결과는 다음과 같이 회전합니다.

참고
Reference
이 문제에 관하여(react-leaflet의 마커 회전), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/two_pack/items/d05d640d6dfa52ccbf72텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)