如何在 React-Leaflet 3 中使用 Leaflet Routing Machine?
How to use Leaflet Routing Machine with React-Leaflet 3?
在 react-leaflet 2.8.0 中做事的旧方法是使用 MapLayer
和 withLeaflet
.
但现在在 react-leaflet 中:
MapLayer
and withLeaflet
are deprecated as of version 3.
我正在努力掌握核心文档:https://react-leaflet.js.org/docs/core-introduction
但以下不起作用,我得到
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;
你用的是createLayerComponent
,但是路由机其实是一个控件。使用 createControlComponent
.
我还建议将其作为单独的自定义组件,如文档中所述,而不是将其放在 useEffect 中。文档很难。请随意阅读 How to extend TileLayer component in react-leaflet v3? 看看这是否有助于理解如何使用 react-leaflet v3 制作自定义组件。
您可以这样做:
import L from "leaflet";
import { createControlComponent } from "@react-leaflet/core";
import "leaflet-routing-machine";
const createRoutineMachineLayer = (props) => {
const instance = L.Routing.control({
waypoints: [
L.latLng(33.52001088075479, 36.26829385757446),
L.latLng(33.50546582848033, 36.29547681726967)
],
...otherOptions
});
return instance;
};
const RoutingMachine = createControlComponent(createRoutineMachineLayer);
export default RoutingMachine;
Working Codesandbox
在 react-leaflet 2.8.0 中做事的旧方法是使用 MapLayer
和 withLeaflet
.
但现在在 react-leaflet 中:
MapLayer
andwithLeaflet
are deprecated as of version 3.
我正在努力掌握核心文档:https://react-leaflet.js.org/docs/core-introduction
但以下不起作用,我得到
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;
你用的是createLayerComponent
,但是路由机其实是一个控件。使用 createControlComponent
.
我还建议将其作为单独的自定义组件,如文档中所述,而不是将其放在 useEffect 中。文档很难。请随意阅读 How to extend TileLayer component in react-leaflet v3? 看看这是否有助于理解如何使用 react-leaflet v3 制作自定义组件。
您可以这样做:
import L from "leaflet";
import { createControlComponent } from "@react-leaflet/core";
import "leaflet-routing-machine";
const createRoutineMachineLayer = (props) => {
const instance = L.Routing.control({
waypoints: [
L.latLng(33.52001088075479, 36.26829385757446),
L.latLng(33.50546582848033, 36.29547681726967)
],
...otherOptions
});
return instance;
};
const RoutingMachine = createControlComponent(createRoutineMachineLayer);
export default RoutingMachine;