如何在 react-leaflet 中使用 setRotationAngle
How to use setRotationAngle in react-leaflet
我正在使用 react-leaflet
地图,运行-时间我必须更新标记的方向,我正在使用 leaflet-rotatedmarker
和 react-leaflet
的库。标记方向在加载和重新加载时工作正常,但在从道具接收值后未更新。
<Marker
key={1}
position={{ lat: position.latitude, lng: position.longitude }}
rotationAngle={position.course}
rotationOrigin="center"
icon={L.icon({
iconUrl: "xyz.svg",
iconSize: [50,50],
iconAnchor: [25, 25]})}
/>
我只想在接收到新的角度值时更新 rotationAngle
。
请指教,我不知道如何在react-leaflet
中使用setRotationAngle
谢谢
如前所述here,不可能再在 v2 中扩展 Marker,因此基本上我们需要 copy-paste 整个 Marker class 并添加此功能。仅供参考,这是我的工作版本 RotatedMarker
:
import React from 'react';
import { Marker as LeafletMarker } from 'leaflet';
import { LeafletProvider, withLeaflet, MapLayer } from 'react-leaflet';
import 'leaflet-rotatedmarker';
class RotatedMarker extends MapLayer {
static defaultProps = {
rotationOrigin: 'center',
};
createLeafletElement(props) {
const el = new LeafletMarker(props.position, this.getOptions(props));
this.contextValue = { ...props.leaflet, popupContainer: el };
return el;
}
updateLeafletElement(fromProps, toProps) {
if (toProps.position !== fromProps.position) {
this.leafletElement.setLatLng(toProps.position);
}
if (toProps.icon !== fromProps.icon) {
this.leafletElement.setIcon(toProps.icon);
}
if (toProps.zIndexOffset !== fromProps.zIndexOffset) {
this.leafletElement.setZIndexOffset(toProps.zIndexOffset);
}
if (toProps.opacity !== fromProps.opacity) {
this.leafletElement.setOpacity(toProps.opacity);
}
if (toProps.draggable !== fromProps.draggable) {
if (toProps.draggable === true) {
this.leafletElement.dragging.enable();
} else {
this.leafletElement.dragging.disable();
}
}
if (toProps.rotationAngle !== fromProps.rotationAngle) {
this.leafletElement.setRotationAngle(toProps.rotationAngle);
}
if (toProps.rotationOrigin !== fromProps.rotationOrigin) {
this.leafletElement.setRotationOrigin(toProps.rotationOrigin);
}
}
render() {
const { children } = this.props;
return children == null || this.contextValue == null ? null : (
<LeafletProvider value={this.contextValue}>{children}</LeafletProvider>
);
}
}
export default withLeaflet(RotatedMarker);
我正在使用 react-leaflet
地图,运行-时间我必须更新标记的方向,我正在使用 leaflet-rotatedmarker
和 react-leaflet
的库。标记方向在加载和重新加载时工作正常,但在从道具接收值后未更新。
<Marker
key={1}
position={{ lat: position.latitude, lng: position.longitude }}
rotationAngle={position.course}
rotationOrigin="center"
icon={L.icon({
iconUrl: "xyz.svg",
iconSize: [50,50],
iconAnchor: [25, 25]})}
/>
我只想在接收到新的角度值时更新 rotationAngle
。
请指教,我不知道如何在react-leaflet
setRotationAngle
谢谢
如前所述here,不可能再在 v2 中扩展 Marker,因此基本上我们需要 copy-paste 整个 Marker class 并添加此功能。仅供参考,这是我的工作版本 RotatedMarker
:
import React from 'react';
import { Marker as LeafletMarker } from 'leaflet';
import { LeafletProvider, withLeaflet, MapLayer } from 'react-leaflet';
import 'leaflet-rotatedmarker';
class RotatedMarker extends MapLayer {
static defaultProps = {
rotationOrigin: 'center',
};
createLeafletElement(props) {
const el = new LeafletMarker(props.position, this.getOptions(props));
this.contextValue = { ...props.leaflet, popupContainer: el };
return el;
}
updateLeafletElement(fromProps, toProps) {
if (toProps.position !== fromProps.position) {
this.leafletElement.setLatLng(toProps.position);
}
if (toProps.icon !== fromProps.icon) {
this.leafletElement.setIcon(toProps.icon);
}
if (toProps.zIndexOffset !== fromProps.zIndexOffset) {
this.leafletElement.setZIndexOffset(toProps.zIndexOffset);
}
if (toProps.opacity !== fromProps.opacity) {
this.leafletElement.setOpacity(toProps.opacity);
}
if (toProps.draggable !== fromProps.draggable) {
if (toProps.draggable === true) {
this.leafletElement.dragging.enable();
} else {
this.leafletElement.dragging.disable();
}
}
if (toProps.rotationAngle !== fromProps.rotationAngle) {
this.leafletElement.setRotationAngle(toProps.rotationAngle);
}
if (toProps.rotationOrigin !== fromProps.rotationOrigin) {
this.leafletElement.setRotationOrigin(toProps.rotationOrigin);
}
}
render() {
const { children } = this.props;
return children == null || this.contextValue == null ? null : (
<LeafletProvider value={this.contextValue}>{children}</LeafletProvider>
);
}
}
export default withLeaflet(RotatedMarker);