首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用带有反应传单3的传单选路机?

如何使用带有反应传单3的传单选路机?
EN

Stack Overflow用户
提问于 2021-05-23 09:46:04
回答 1查看 4.1K关注 0票数 4

MapLayerwithLeaflet来做反应的旧方法--传单2.8.0。

但现在在反应-传单:

MapLayerwithLeaflet在第3版时就不再受欢迎了。

我试图掌握核心:https://react-leaflet.js.org/docs/core-introduction的文档

但是下面这些都不起作用,我

提供的对象不是一个层。

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-23 15:07:30

您使用的是createLayerComponent,但是路由机实际上是一个控件。使用createControlComponent

我还建议将其作为一个单独的自定义组件,如文档中所述,而不是将其放入useEffect中。医生很难。请随意阅读如何在react v3中扩展v3组件?,看看这是否有助于理解如何使用leaflet v3制作自定义组件。

以下是你的做法:

代码语言:javascript
复制
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;

工作码箱

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67658340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档