The old way of doing things in react-leaflet 2.8.0 was to use MapLayer
and withLeaflet
.
But now in react-leaflet:
MapLayer
andwithLeaflet
are deprecated as of version 3.
I'm trying to grasp the documentation for core: https://react-leaflet.js.org/docs/core-introduction
but the following doesn't work, I get
The provided object is not a Layer.
import React, { Component, useEffect } from "react";
import { useLeafletContext, leafletElement, createLayerComponent } from '@react-leaflet/core'
import { MapContainer, TileLayer, useMap } from "react-leaflet";
import Leaflet from "leaflet";
import "leaflet-routing-machine";
function Routing(props) {
const context = useLeafletContext();
useEffect(() =>
{
let routing = createLayerComponent(Leaflet.Routing.control(
{
waypoints: [
Leaflet.latLng(33.52001088075479, 36.26829385757446),
Leaflet.latLng(33.50546582848033, 36.29547681726967)
],
lineOptions: {
styles: [{ color: "#6FA1EC", weight: 4 }]
},
show: false,
addWaypoints: false,
routeWhileDragging: true,
draggableWaypoints: true,
fitSelectedRoutes: true,
showAlternatives: false
}), )
const container = context.layerContainer || context.map
container.addLayer(routing)
return () => {
container.removeLayer(routing)
}
})
return null;
}
function MapComponent() {
return (
<MapContainer center={[33.5024, 36.2988]} zoom={14} >
<TileLayer url="https://api.maptiler.com/maps/ch-swisstopo-lbm-dark/256/{z}/{x}/{y}.png?key=gR2UbhjBpXWL68Dc4a3f" />
<Routing />
</MapContainer>
);
}
export default MapComponent;