首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在react-leaflet中使用SnakeAnim

如何在react-leaflet中使用SnakeAnim
EN

Stack Overflow用户
提问于 2020-06-05 10:23:38
回答 1查看 448关注 0票数 1

我想使用Leaflet.Polyline.SnakeAnim,我有一个问题。有没有在react-leaflet中创建自定义组件的例子?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-05 16:52:22

安装库并导入leaflet.polyline.snakeanim/L.Polyline.SnakeAnim.js

然后创建一个包装器来调用触发动画所需的方法。您可以使用startAnimation道具来触发动画,但您可以根据需要进行调整:

代码语言:javascript
复制
... // imports here

const SnakeAnim = ({ startAnimation }) => {
  const { map } = useLeaflet();

  useEffect(() => {
    if (!startAnimation) return;
    const trd = [63.5, 11];
    const mad = [40.5, -3.5];
    const lnd = [51.5, -0.5];
    const ams = [52.3, 4.75];
    const vlc = [39.5, -0.5];

    const route = L.featureGroup([
      L.marker(trd, { icon }),
      L.polyline([trd, ams]),
      L.marker(ams, { icon }),
      L.polyline([ams, lnd]),
      L.marker(lnd, { icon }),
      L.polyline([lnd, mad]),
      L.marker(mad, { icon }),
      L.polyline([mad, vlc]),
      L.marker(vlc, { icon })
    ]);

    map.fitBounds(route.getBounds());

    map.addLayer(route);

    route.snakeIn();

    route.on("snakestart snake snakeend", ev => {
      console.log(ev.type);
    });
  }, [startAnimation]);

  return null;
};

像这样使用它,在react-leaflet的Map包装器中导入包装器:

代码语言:javascript
复制
const [startAnimation, setStartAnimation] = useState(false);
  const startSnake = () => setStartAnimation(true);

  return (
    <>
      <Map center={position} zoom={zoom} style={{ height: "90vh" }}>
        <TileLayer
          attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
        <SnakeAnim startAnimation={startAnimation} />
      </Map>
      <button onClick={startSnake}>Snake it!</button>
    </>
  );

创建一个btn,添加一个侦听器,并通过一个标志触发动画。

Demo

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

https://stackoverflow.com/questions/62207110

复制
相关文章

相似问题

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