如何在react 16.4.1中使用leaflet-polylinedecorator

2024-04-22

我正在尝试在react 16.4.1中使用传单插件polylinedecorator(所以没有钩子)。然而,我能找到的关于如何在 React 中使用此插件的唯一示例是使用钩子(请参阅:如何将 Polylinedac​​orator 与 React leaflet 一起使用 https://stackoverflow.com/questions/56411133/how-to-use-polylinedacorator-with-react-leaflet/56431136#56431136),并且我不确定如何调整它以便能够在我的代码中使用它。

到目前为止我所拥有的是这个 polylinedecorator 组件:

import React, { Component } from "react";
import { Polyline } from "react-leaflet";
import L from "leaflet";
import "leaflet-polylinedecorator";

export default class PolylineDecorator extends Component {
  componentDidUpdate() {
    if (this.props.map) {
        const polyline = L.polyline(this.props.positions).addTo(this.props.map);

        L.polylineDecorator(polyline, {
            patterns: [
                {
                  offset: "100%",
                  repeat: 0,
                  symbol: L.Symbol.arrowHead({
                    pixelSize: 15,
                    polygon: false,
                    pathOptions: { stroke: true }
                  })
                }
              ]
        }).addTo(this.props.map);
    }
  }

  render() {
    return <></>;
  }
}

我正在这样使用:

import React, { Component } from "react";
import { Polyline, LayerGroup } from "react-leaflet";
import PolylineDecorator from "./PolylineDecorator";

export default class RouteLayer extends Component {
  render() {
    const { beginLocations } = this.props;
    const locations = [];
    const differentLocations: [];

    beginLocations.forEach((location, index) => {
      // some filtering going on here and pushing the locations to one of the 
         two arrays (locations, differentLocations)
    });

    return (
      <LayerGroup>
        <PolylineDecorator
          map={this.props.map}
          positions={locations}
          color="#4e5c8d"
          interactive={false}
        />
        <PolylineDecorator
          map={this.props.map}
          positions={differentFloorLinesLocations}
          color="red"
          interactive={false}
        />
      </LayerGroup>
    );
  }
}

RouteLayer 嵌套在地图内,如下所示(为简单起见,省略了一些组件):

 <LeafletMap
     ref={r => {
       this.map = r;
       if (this.props.setRefMap) {
         this.props.setRefMap(r);
       }
     }}>
     <RouteLayer
        map={this.map ? this.map.leafletElement : null}
        locations={locations}
      />
 </LeafletMap>

现在,折线已绘制,但并不完全符合预期,因为过滤似乎不起作用(当我只使用没有装饰器的折线时,此过滤工作正常)。 我试图用来装饰线条的箭头出现了,所以这很好。但是,我对 PolylineDecorator 类现在的外观不满意,这似乎不是执行此操作的正确方法。 我也不确定以我在这里所做的方式传递对地图的引用是否正确。 任何有关如何使这项工作正确进行的建议都将受到赞赏。


对于反应版本< 16.8以下组件演示了如何集成L.polylineDecorator https://github.com/bbecquet/Leaflet.PolylineDecorator with React-Leaflet https://react-leaflet.js.org/:

import React, { Component } from "react";
import { Polyline, withLeaflet } from "react-leaflet";
import L from "leaflet";
import "leaflet-polylinedecorator";

class PolylineDecorator extends Component {
  constructor(props) {
    super(props);
    this.polyRef = React.createRef();
  }
  componentDidMount() {
    const polyline = this.polyRef.current.leafletElement; //get native Leaflet polyline
    const { map } = this.polyRef.current.props.leaflet; //get native Leaflet map

    L.polylineDecorator(polyline, {
      patterns: this.props.patterns
    }).addTo(map);
  }

  render() {
    return <Polyline ref={this.polyRef} {...this.props} />;
  }
}

export default withLeaflet(PolylineDecorator);

Usage

export default class MyMap extends Component {
  render() {
    const { center, zoom } = this.props;

    const polyline = [[57, -19], [60, -12]];

    const arrow = [
      {
        offset: "100%",
        repeat: 0,
        symbol: L.Symbol.arrowHead({
          pixelSize: 15,
          polygon: false,
          pathOptions: { stroke: true }
        })
      }
    ];

    return (
      <Map center={center} zoom={zoom}>
        <TileLayer url="http://{s}.tile.osm.org/{z}/{x}/{y}.png" />
        <PolylineDecorator patterns={arrow} positions={polyline} />
      </Map>
    );
  }
}

这是一个演示 https://stackblitz.com/edit/react-q68g8p

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在react 16.4.1中使用leaflet-polylinedecorator 的相关文章

随机推荐

  • ArrayIndexOutOfBoundsException - 改造调用

    我正在尝试打电话PUT方法在我的Retrofit实例 Response
  • 大端和小端表示

    如果我写以下内容 section data align 4 X db 1 Y dw 5 Z db 0x11 section text add dword X 0xAA000101 我试图理解大端和小端表示之间的差异 但我不明白每个表示的每个
  • 使用react-google-maps-api将自定义标记作为React组件

    我们正在转换自google map react https github com google map react google map react readme to react google maps api这意味着更多原生 Googl
  • 使用 COMPS 构建 C/C++ 应用程序时出错:硬编码路径

    我正在尝试构建一个使用 C C 绑定开发的 COMPASs 应用程序 当我构建应用程序时 出现以下错误 您知道我该如何解决这个问题吗 xxxx xxx c increment gt buildapp increment BSC Barcel
  • Crash Firebase vs Crashlytics vs HockyApp

    我一直在应用程序中使用 Crashlytics 我们的客户端使用 HockeyApp 并且我是通过 Google Firebase 最近的更新了解到的 有谁有机会使用上述工具 您的概述和建议是什么 您是否喜欢其中一个 为什么 祝你有美好的一
  • Laravel 将数据传递给路由

    我是 Laravel 5 的新手 我需要一些帮助 因为我无法解决我的简单问题 我有一个数据列表http sample com partners http sample com partners如果我单击数据链接 我希望将其重定向到 http
  • 如何从服务器中的客户端池中识别客户端 - 设计

    好的 我正在使用一个客户端 服务器库 服务器可以接受来自客户端的多个连接 每次调用 Accept 时 如果成功 客户端套接字的句柄就会被推送到映射 并以假定的唯一 ID 作为键 这样当服务器稍后想要向特定客户端发送消息时 它就会使用该 ID
  • mysql 其中字符串以数字结尾

    我的表列包含如下值 id item 1 aaaa11a112 2 aa1112aa2a 3 aa11aa1a11 4 aaa2a222aa 我只想选择项目值以数字结尾的行 有这样的事吗 select from table where ite
  • Maven 对特定存储库使用 SOCKS 代理

    我试图将 Maven 配置为在访问特定存储库时使用袜子代理 但不将其用于 Maven 中央存储库 我用过export MAVEN OPTS DsocksProxyHost
  • 单击单个单元格并使用 Javascript 添加颜色到 HTML 表格

    我创建了一个 HTML 表 带有用户输入的列和行 并且还具有选择颜色的动态方式 现在我希望能够单击表格中的各个单元格并用所选颜色为它们着色 到目前为止我有这个代码 我的最终目标是当我再次点击 提交 时能够重置颜色 流程将是 选择桌子尺寸 选
  • 当其父对象设置为不显示时,如何获取对象的最小高度?

    为什么我无法获取min height当一个物体的parent被设定为display none 但如果最小高度是 我仍然可以得到物体的高度not in use 例如 css li display none object display blo
  • 确定 Linux 或 Windows 是 C++ 语言

    我正在用 C 编写一个跨平台兼容函数 该函数根据输入文件名创建目录 我需要知道机器是 Linux 还是 Windows 并使用适当的正斜杠或反斜杠 对于下面的代码 如果机器是Linux那么isLinux true 如何确定操作系统 bool
  • 再次将服务注入域对象

    我有一个对地理数据进行操作的特定域 我正在 TypeScript 和 NodeJS 中实现这个项目 并有以下类 Point 包含纬度和经度的值对象 Area 包含点集作为形状定义的值对象 Sector 实体 它不是持久的 但它是可变的 包含
  • 提交表单不会在 jquery ajax 调用中停止

    我得到以下代码 ajax type POST async false url CheckIdExist data param success function result if result true return false error
  • 如何解析不寻常的日期字符串

    您好 我有一个不寻常的日期格式 我想将其解析为 DateTime 对象 string date 20101121 2010 11 21 string time 13 11 41 HH mm ss 我想用DateTime Tryparse 但
  • Android.mk 与 Application.mk

    我对 Android mk 和 Application mk 的使用有点模糊 我尝试阅读 APPLICATION MK HTML ANDROID MK HTML 在NDK 附带的文档 http developer android com s
  • 如何将类元数据转换为 JSON 字符串

    如何生成类元数据的 JSON for eg C 类 public class Product public int Id get set public string Name get set public bool IsActive get
  • WebClient 最大连接池限制?

    如果远程服务阻塞 我可以发送多少个并发请求 意思是 什么是最大连接数spring在使用时内部使用的池限制WebClient Autowired private WebClient webClient webClient post uri u
  • 卸载 Magento 自定义扩展及其数据库

    我创建了一个扩展并创建了扩展的 zip 文件 以便我可以通过 Magento Connect 安装它 当我使用 Magento Connect 卸载自定义扩展时 它只会删除我的扩展文件和文件夹 但不会删除我的扩展的数据库表 我还想删除我的数
  • 如何在react 16.4.1中使用leaflet-polylinedecorator

    我正在尝试在react 16 4 1中使用传单插件polylinedecorator 所以没有钩子 然而 我能找到的关于如何在 React 中使用此插件的唯一示例是使用钩子 请参阅 如何将 Polylinedac orator 与 Reac