I am using react-leaflet maps, and run-time I have to update the direction of markers, I am using a library of leaflet-rotatedmarker with react-leaflet. markers direction are working fine on load and reload but not updated after receiving values from props.
<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]})}
/>
I just want to update rotationAngle on receiving new angle value.
Please Advise, I don't know how to use setRotationAngle in react-leaflet
Thanks
As mentioned here, it's not possible anymore to extend Marker in v2, so basically we need to copy-paste the whole Marker class and add this functionality. FYI, this is my working version of 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);
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With