integration with react-leaflet #860
Replies: 10 comments 4 replies
-
@BjoernRave at first you need import leaflet.pm files at root file of your project
Then you need add ref to yours map component
after this you can access map object in componentDidMount() and add toolbar and listeners to pm actions
|
Beta Was this translation helpful? Give feedback.
-
@TPABHuKOB first of all thanks alot for helping me, but when I try to import leaflet.pm I get the error |
Beta Was this translation helpful? Give feedback.
-
@BjoernRave I'm using leaflet.pm with nuxtJS, which is the next.js equivalent in the vue.js world. What's important is that leaflet and leaflet.pm are only initiated when the context of your application is the browser. In next.js, I assume your import of leaflet.pm is executed on the serverside. That might be handled by Import leaflet.pm in the browser context only and it should work fine. (Will close the issue as it's not a problem to fix here but we can keep the discussion going to help you) |
Beta Was this translation helpful? Give feedback.
-
I'm trying to integrate to react too, but I get |
Beta Was this translation helpful? Give feedback.
-
In case someone still has this issue. Here is an example of how I integrated with import React from 'react';
import { Map, MapProps } from 'react-leaflet';
import L from 'leaflet';
import '@geoman-io/leaflet-geoman-free';
import '@geoman-io/leaflet-geoman-free/dist/leaflet-geoman.css';
type PMDrawCircleEvent = { layer: L.Circle & { pm: { enable: () => void } } };
type PMEditCircleEvent = { target: L.Circle };
interface Props extends MapProps {
onSelectionCircleAdded: (latLang: L.LatLng, radius: number) => void;
onSelectionCircleMoved: (latLang: L.LatLng, radius: number) => void;
onSelectionCircleRemoved: () => void;
}
const MapWithGeoman: React.FC<Props> = (props) => {
const {
children,
onSelectionCircleAdded: onCircleAdded,
onSelectionCircleMoved: onCircleMoved,
onSelectionCircleRemoved: onCircleRemoved,
...mapProps
} = props;
const leafletMapRef = React.useRef<Map>(null);
const filtersLayerRef = React.useRef<L.LayerGroup<L.Circle>>();
React.useEffect(() => {
if (leafletMapRef.current) {
const mapElement = leafletMapRef.current.leafletElement;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(mapElement as any).pm.addControls({
drawMarker: false,
drawCircleMarker: false,
drawPolyline: false,
drawRectangle: false,
drawPolygon: false,
editMode: false,
dragMode: false,
cutPolygon: false,
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(mapElement as any).pm.setGlobalOptions({ pmIgnore: false });
filtersLayerRef.current = L.layerGroup().addTo(mapElement);
mapElement.on('pm:create', (e) => {
// only show one selection circle
filtersLayerRef.current?.clearLayers();
if (
e.layer &&
e.layer.pm &&
filtersLayerRef.current?.getLayers.length === 0
) {
const circle = (e as unknown) as PMDrawCircleEvent;
// enable editing of circle
circle.layer.pm.enable();
filtersLayerRef.current?.addLayer(circle.layer);
onCircleAdded(circle.layer.getLatLng(), circle.layer.getRadius());
circle.layer.on('pm:edit', (e) => {
const event = (e as unknown) as PMEditCircleEvent;
onCircleMoved(event.target.getLatLng(), event.target.getRadius());
});
}
});
mapElement.on('pm:remove', () => {
onCircleRemoved();
});
}
});
return (
<Map ref={leafletMapRef} {...mapProps}>
{children}
</Map>
);
};
export default MapWithGeoman; |
Beta Was this translation helpful? Give feedback.
-
@jeppe-style is there codesandbox available for playing with it. !! |
Beta Was this translation helpful? Give feedback.
-
@ddprajapati I created a code sandbox here. Note that it at the moment it only works with v2.5. Filed a separate issue about that #640. |
Beta Was this translation helpful? Give feedback.
-
Hi this react geoman code is not working properly and i need an arc layer
|
Beta Was this translation helpful? Give feedback.
-
Fixed in Release: |
Beta Was this translation helpful? Give feedback.
-
Just downloaded the code from Sandbox and tried to start, but I'm getting this error:
Could you please help? |
Beta Was this translation helpful? Give feedback.
-
Hey, I was wondering if this library is compatible with react-leaflet or if somebody knows how to make it work with react (and maybe even next.js)
Beta Was this translation helpful? Give feedback.
All reactions