如何将polylinedac​​orator与react传单一起使用

时间:2019-06-01 23:26:49

标签: reactjs react-leaflet

我已经使用react-leaflet创建了折线,我想使用polylinedac​​orator显示折线上的反应,但是我不知道如何使用react-leaflet进行折线。我发现了多个带有传单的示例,但没有找到react-leaflet

const polyline = [[51.505, -0.09], [51.51, -0.1], [51.51, -0.12]]

export default class VectorLayersExample extends Component<{}> {
  render() {
    return (
      <Map center={center} zoom={13}>
       <TileLayer
          attribution='&amp;copy <a 
            href="http://osm.org/copyright">OpenStreetMap</a> 
            contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
      />
    <Polyline color="lime" positions={polyline} />
    </Map>
 )
 }

任何人都可以告诉我如何在上述代码中使用多行修饰符

1 个答案:

答案 0 :(得分:0)

Leaflet.PolylineDecorator可以与React-Leaflet集成,如下所示:

a)安装leafletleaflet-polylinedecorator软件包:npm i leaflet leaflet-polylinedecorator

b)安装后,以下组件演示了如何将Polyline组件与L.polylineDecorator一起使用:

import React, { useRef, useEffect } from "react";
import { Map, TileLayer, Polyline, withLeaflet } from "react-leaflet";
import L from "leaflet";
import "leaflet-polylinedecorator";

const PolylineDecorator = withLeaflet(props => {
  const polyRef = useRef();
  useEffect(() => {
    const polyline = polyRef.current.leafletElement; //get native Leaflet polyline
    const { map } = polyRef.current.props.leaflet; //get native Leaflet map

    L.polylineDecorator(polyline, {
        patterns : props.patterns
    }).addTo(map);
  }, []);
  return <Polyline ref={polyRef} {...props} />;
});

用法

function MyMap(props) {
  const { center, zoom } = 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>
  );
}